Font
用于设置单元格的字体格式,例如:
Dim App As New MSExcel.Application
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open("D:\Report.xls")
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
Dim Rg As MSExcel.Range = Ws.Range("A1")
Rg.Value = "Foxtable操控Excel全攻略"
With Rg.Font
.Name = "黑体" '字体
.Size = 20 '字号
.Bold = True '加粗
.Italic = True '斜体
.ColorIndex = 3'颜色
End With
App.Visible = True
其它字体设置:
上标:Rg.Font.Superscript = True
下标:Rg.Font.Subscript = True
单下划线:Rg.Font.Underline = MSExcel.XlUnderlineStyle.xlUnderlineStyleSingle
双下划线:Rg.Font.Underline = MSExcel.XlUnderlineStyle.xlUnderlineStyleDouble
会计用单下划线:Rg.Font.Underline = MSExcel.XlUnderlineStyle.xlUnderlineStyleSingleAccounting
会计用双下划线:Rg.Font.Underline = MSExcel.XlUnderlineStyle.xlUnderlineStyleDoubleAccounting
取消下划线:Rg.Font.Underline = MSExcel.XlUnderlineStyle.xlUnderlineStyleNone
设置单元格字符串中一部分字符的字体格式:
利用Range对象的Characters属性,定位到要设置格式的字符,然后利用Font对其进行设置。Characters中的第一个参数为起始位置,第二个参数为字符长度。如,下面的代码就是对“Foxtable操控Excel全攻略”中的“操控”两字进行格式设置:
Dim App As New MSExcel.Application
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open("D:\Report.xls")
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
Dim Rg As MSExcel.Range = Ws.Range("A1")
Rg.Value = "Foxtable操控Excel全攻略"
Dim Chr As MSExcel.Characters = Rg.Characters(9,2)
With Chr.Font
.Name = "黑体" '字体
.Size = 20 '字号
.Bold = True '加粗
.Italic = True '斜体
.ColorIndex = 3'颜色
End With
App.Visible = True
需要说明的是,部分字符串同样可以设置上标、下标和下划线。
http://www.foxtable.com/webhelp/scr/2121.htm