高分求助!vb.net如何设置excel中标签Label的字体大小
在excelSheet中,excelSheet.Label1.Caption="A1"'这样赋值是可以的但是在改变字体大小时出错了,Dimlfont=NewFont("S...
在excelSheet中, excelSheet.Label1.Caption = "A1" '这样赋值是可以的
但是在改变字体大小时出错了,
Dim lfont = New Font("SimHei", 20, New FontStyle)
excelSheet.Label1.Font = lfont '这里就出异常了
excel模版太多了,1W多个,自能在程序中更改。
求助高手,这个问题怎么解决?
以下是相关代码:
Private Function outDataToExcel(ByVal objPlan As PlanT, ByVal strFileName As String) As Boolean
Dim excelApp As Object
Dim excelBook As Object
Dim excelSheet As Object
Dim intLoopi As Integer 'excelBook.Worksheets.Count
Dim intLoopj As Integer 'objPlan.Qty / (20 + 1)
'Microsoft.Office.Tools.Excel.Controls.Label
'Dim lfont = New Font("SimHei", 20, New FontStyle)
Try
excelApp = CreateObject("Excel.Application")
excelApp.Visible = False
excelApp.UserControl = False
excelApp.DisplayAlerts = False
'excelApp = New Excel.Application
excelBook = excelApp.Workbooks.Open(strFileName)
excelApp.Visible = False
For intLoopi = 1 To excelBook.Worksheets.Count
excelSheet = excelBook.Sheets.Item(intLoopi)
'excelSheet.Label1.Font = lfont
excelSheet.Label1.Caption = "A1"
'excelSheet.Label2.Caption = objPlan.PlanId
'excelSheet.Label3.Caption = objPlan.MakeDate.Month & "-" & objPlan.MakeDate.Day
'excelSheet.save(strFileName)
'excelApp.Visible = True
excelSheet.PrintOut()
System.Threading.Thread.Sleep(1000 * 2)
Next intLoopi
'excelApp.Workbooks(1).Close(SaveChanges:=False)
'excelApp.Close(Type.Missing, Type.Missing, Type.Missing)
excelApp.Workbooks.Close()
excelApp.Quit()
'excelApp.UserControl = True
MRComObject(excelSheet)
MRComObject(excelBook)
MRComObject(excelApp)
GC.Collect()
System.Threading.Thread.Sleep(1000 * 3)
Return True
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
excelApp.Quit()
excelSheet = Nothing
excelBook = Nothing
excelApp = Nothing
MRComObject(excelSheet)
MRComObject(excelBook)
MRComObject(excelApp)
Return False
End Try
End Function 展开
但是在改变字体大小时出错了,
Dim lfont = New Font("SimHei", 20, New FontStyle)
excelSheet.Label1.Font = lfont '这里就出异常了
excel模版太多了,1W多个,自能在程序中更改。
求助高手,这个问题怎么解决?
以下是相关代码:
Private Function outDataToExcel(ByVal objPlan As PlanT, ByVal strFileName As String) As Boolean
Dim excelApp As Object
Dim excelBook As Object
Dim excelSheet As Object
Dim intLoopi As Integer 'excelBook.Worksheets.Count
Dim intLoopj As Integer 'objPlan.Qty / (20 + 1)
'Microsoft.Office.Tools.Excel.Controls.Label
'Dim lfont = New Font("SimHei", 20, New FontStyle)
Try
excelApp = CreateObject("Excel.Application")
excelApp.Visible = False
excelApp.UserControl = False
excelApp.DisplayAlerts = False
'excelApp = New Excel.Application
excelBook = excelApp.Workbooks.Open(strFileName)
excelApp.Visible = False
For intLoopi = 1 To excelBook.Worksheets.Count
excelSheet = excelBook.Sheets.Item(intLoopi)
'excelSheet.Label1.Font = lfont
excelSheet.Label1.Caption = "A1"
'excelSheet.Label2.Caption = objPlan.PlanId
'excelSheet.Label3.Caption = objPlan.MakeDate.Month & "-" & objPlan.MakeDate.Day
'excelSheet.save(strFileName)
'excelApp.Visible = True
excelSheet.PrintOut()
System.Threading.Thread.Sleep(1000 * 2)
Next intLoopi
'excelApp.Workbooks(1).Close(SaveChanges:=False)
'excelApp.Close(Type.Missing, Type.Missing, Type.Missing)
excelApp.Workbooks.Close()
excelApp.Quit()
'excelApp.UserControl = True
MRComObject(excelSheet)
MRComObject(excelBook)
MRComObject(excelApp)
GC.Collect()
System.Threading.Thread.Sleep(1000 * 3)
Return True
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
excelApp.Quit()
excelSheet = Nothing
excelBook = Nothing
excelApp = Nothing
MRComObject(excelSheet)
MRComObject(excelBook)
MRComObject(excelApp)
Return False
End Try
End Function 展开
2个回答
展开全部
这是因为你的程序的问题。你可以仔细看一下,Font是一个Interface,它怎么可以被实例化呢??另外,个人认为如果在一种方法上实现不了的话,那就换一种方式,因为Font是一个Interface,所以在它中间定义了很多的属性,那就可以把你想谁知的属性一个个的点出来啊,
比如:
excelSheet.Label1.Font.Name="XXX"
excelSheet.Label1.Underline = True
等等,你可以自己尝试嘛,不要怕报错,只有错了才会学到东西,个人感想。
比如:
excelSheet.Label1.Font.Name="XXX"
excelSheet.Label1.Underline = True
等等,你可以自己尝试嘛,不要怕报错,只有错了才会学到东西,个人感想。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询