试图让一个基本的 VB.NET Excel 文件创建应用程序正常工作,从这里的一些示例代码开始,我得到了这个代码的错误消息:
Private Sub ButtonCreateExcelFile_Click( sender As Object, e As EventArgs) Handles ButtonCreateExcelFile.Click
Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkBook.Application.Visible = True
xlWorkBook.Application.ScreenUpdating = False
xlWorkSheet = xlWorkBook.Sheets("sheet1")
'xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
xlWorkSheet.SaveAs("C:\\misc\\vbexcel.xls")
. . .
我首先尝试过这个:
xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
(未能逃脱回击),并且这个(“vbexcel.xlsx”)在某种程度上被误认为“52587100”,如您在此处看到的:
System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2146827284
HelpLink=C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM
HResult=-2146827284
Message=Microsoft Office Excel cannot access the file 'C:\52587100'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
Source=Microsoft Office Excel. . .
通过向路径添加子文件夹和转义字符,如下所示:
xlWorkSheet.SaveAs("C:\\misc\\vbexcel.xlsx")
...我遇到了类似的错误,但这次它只是说它无法访问该文件(没有声称文件名被命名为“52587100”):
System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2146827284
HelpLink=C:\Program Files (x86)\Microsoft Office\Office12\1033\XLMAIN11.CHM
HResult=-2146827284
Message=The file could not be accessed. Try one of the following:
这段代码有什么问题,导致文件无法访问?