16,722
社区成员




Option Explicit On
Imports Microsoft.Office.Interop
Public Class ServiceTest
Private Sub ExcelTest()
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Microsoft.Office.Interop.Excel.Application
xlApp.DisplayAlerts = False
xlApp.SheetsInNewWorkbook = 1
xlApp = CreateObject("Excel.Application") '备注1:如果不加入这句和下句代码会
xlApp.DefaultSaveFormat = Excel.XlFileFormat.xlExcel8 '备注1:在xlWorkBook = xlApp.Workbooks.Add报错
xlWorkBook = xlApp.Workbooks.Add '备注1:稍后我把报错信息附在后面。
xlWorkSheet = xlWorkBook.Sheets.Item(1)
With xlWorkSheet
.Activate()
.Name = "进厂物料总帐"
.Cells.Select()
.Cells.NumberFormatLocal = "@"
.Cells.VerticalAlignment = Excel.Constants.xlCenter
.Cells.HorizontalAlignment = Excel.Constants.xlCenter
.Cells.WrapText = True
.Cells.Font.Size = 11
.Cells.Font.Name = "宋体"
.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape
.PageSetup.LeftMargin = xlApp.InchesToPoints(0.31496062992126)
.PageSetup.RightMargin = xlApp.InchesToPoints(0.31496062992126)
.PageSetup.CenterHorizontally = True
.PageSetup.Zoom = 96
.PageSetup.PrintTitleRows = "$1:$5"
.Range("A2:N2").Merge()
.Range("A2").RowHeight = 26.25
.Range("A2").Font.Size = 16
.Range("A2").Font.Bold = True
.Cells(2, 1) = "进厂物料总帐"
.Range("A3:C3").Merge()
.Range("A3").RowHeight = 12.75
.Range("A3").HorizontalAlignment = Excel.Constants.xlLeft
.Cells(3, 1) = "年度:" & Year(DateTime.Now).ToString.Trim & "年"
.SaveAs("C:\1.xlsx") '备注2:如果程序正常执行至这句代码后也会报错,
'也尝试过在其后面对应的加入参数都是报一样的错错误
'我也试过把这段代码放到Windows窗体里执行是没有任何的错误的,直接就导出数据,
'也不需要在SaveAs后面加入参数,只需要给个保存的路径和文件名就可以了
'.SaveAs(Filename:="c:\ST\1.xls", FileFormat:=Excel.XlFileFormat.xlOpenXMLWorkbook, CreateBackup:=False)
End With
'xlWorkBook.SaveAs("C:\1.xlsx", Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, False, False, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
xlWorkBook = Nothing
xlApp.Application.Workbooks.Close()
xlApp.SheetsInNewWorkbook = 3
xlApp.Quit()
xlApp = Nothing
End Sub
Protected Overrides Sub OnStart(ByVal args() As String)
ExcelTest()
End Sub
End Class