如何用VB.NET控制Excel單元格裡的內容?

1.現在數據是可以發送到Excel單元格裡了,但我想用代碼控制某一單元格字體大一些?(如:fontsize=12 or fontsize=16)

2.如何控制單元格的內容靠左或居中或靠右?

3.如何控制單元下邊框有下劃線,也就是下邊框在打印時能夠打印出來?

請各位大夾指教,謝謝!

---------------------------------------------------------------

你要的功能都在里面,慢慢看...

Public Sub SetSheetStyles(ByVal xlSheet As Excel.Worksheet, _
ByVal rowCount As Int32, _
ByVal colCount As Int32)
Dim i, intStart As Int32

With xlSheet

If AllowHeaderColor Then .Range(.Cells(1, 1), .Cells(1, MasterColumnCount)).Interior.Color = GetHeaderBackColorRGB

If IsExistMasterD Then '主从表
intStart = 5

.Range(.Cells(1, 1), .Cells(1, MasterColumnCount)).Font.Size = 10 '标头字体大小
.Range(.Cells(2, 1), .Cells(2, MasterColumnCount)).Font.Size = 10 '主数据字体大小

.Range(.Cells(1, 1), .Cells(1, MasterColumnCount)).Font.Bold = True '标题字体加粗
.Range(.Cells(2, 1), .Cells(2, MasterColumnCount)).Font.Italic = True '主数据字体斜体

.Range(.Cells(1, 1), .Cells(1, MasterColumnCount)).Borders.LineStyle = 1 '粗边框(已取消)
.Range(.Cells(2, 1), .Cells(2, MasterColumnCount)).Borders.LineStyle = 1 '粗边框(已取消)

If AllowHeaderColor Then .Range(.Cells(4, 1), .Cells(4, colCount)).Interior.Color = GetHeaderBackColorRGB
.Range(.Cells(4, 1), .Cells(rowCount + 4, colCount)).Borders.LineStyle = 1 '主数据边框样式

.Range(.Cells(4, 1), .Cells(4, colCount)).Font.Bold = True '标题字体加粗
.Range(.Cells(4, 1), .Cells(4, colCount)).Font.Size = 10 '数据项字体大小

.Range(.Cells(5, 1), .Cells(rowCount + 4, colCount)).Font.Size = 9 '数据项字体大小

Else
intStart = 2

.Range(.Cells(1, 1), .Cells(1, colCount)).Font.Size = 10 '标头字体大小
.Range(.Cells(1, 1), .Cells(1, colCount)).Font.Bold = True '标题字体加粗

.Range(.Cells(1, 1), .Cells(rowCount + 1, colCount)).Borders.LineStyle = 1 '设表格边框样式
.Range(.Cells(2, 1), .Cells(rowCount + 1, colCount)).Font.Size = 9 '数据项字体大小

End If

If exportSet.IsApply_PCR_ToExcelWord AndAlso exportSet.IsApplyExcel Then '允许使用颜色区分
If rowCount > 2 AndAlso exportSet.IsAlwaysQuestion_ApplyPCRToEW Then '先询问是否执行颜色区分
If MessageBox.Show("数据转换到Excel文档已完成!是否现在就执行奇偶行颜色区分?这可能需要较长的时间。" & vbLf & "如果不再希望出现此提示,请转到 打印设置的高级选项卡。", "总是询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.No Then Return
End If
If rowCount > 2 Then Common.Common.OnInfo("开始用颜色来区分奇偶数据行,这可能需要较长的一段时间。请稍候...", Common.InfoBase.InfoTypeEnum.Working)

Dim bolP As Boolean = (((rowCount - 1) Mod 2) = 0) '减去标头行Count - 1 True=偶行 Even,False=奇行 Odd

Dim startD As Double = Microsoft.VisualBasic.Timer
Dim intEBC, intOBC As Int32
intEBC = GetEvenBackColorRGB
intOBC = GetOddBackColorRGB

Dim intS As Int32 = intStart
rowCount += intS - 1
For i = intStart To rowCount
If bolP Then '偶行
.Range(.Cells(i, 1), .Cells(i, colCount)).Interior.Color = intEBC
Else '奇行
.Range(.Cells(i, 1), .Cells(i, colCount)).Interior.Color = intOBC
End If

bolP = Not bolP
Next i

If rowCount > 2 Then Common.Common.OnInfo("颜色区分奇偶数据行已完成!用时:" & (Microsoft.VisualBasic.Timer - startD).ToString & " 秒", Common.InfoBase.InfoTypeEnum.Message)
End If

End With

With xlSheet.PageSetup
If MasterColumnCount > 6 Then
If MasterColumnCount > 12 Then .PaperSize = Excel.XlPaperSize.xlPaperA3 '超过12列就用A3纸

.Orientation = Excel.XlPageOrientation.xlLandscape '横幅

Else
.Orientation = Excel.XlPageOrientation.xlPortrait '竖幅A4纸
End If

' .TopMargin = 1'不改变,因为有Header
'.BottomMargin = 1
.LeftMargin = 1
.RightMargin = 1

.CenterHorizontally = True '水平居中对齐
' .LeftHeaderPicture.Filename = Application.StartupPath & "\Resources\TRI-T Icon.gif"
.LeftHeader = "&""宋体,Bold""&13" & "TRI-T Company Limited" '公司
.CenterHeader = "&""宋体,Bold""&13" & TitleText '标题"
.LeftFooter = "&""宋体""&10制表人:" & PreparedBy '制表人
.CenterFooter = "&""宋体""&10制表日期:" & GetDatasheetDate
.RightFooter = "&""宋体""&10第&P页 共&N页"
End With

End Sub

---------------------------------------------------------------

3tzjq(永不言弃)
大哥讲的很详细了,我没什么要说了!!!

Published At
Categories with Web编程
Tagged with
comments powered by Disqus