VB.net Excel对象写入到它
我正在尝试编写一个简化的库,用于从vb.net应用程序读写excel 我只能在使用CreateWorkbook过程后写入excel文件。我无法使用AccessXLFile过程写入excel文件 有人能看一下吗VB.net Excel对象写入到它,excel,vb.net,object,Excel,Vb.net,Object,我正在尝试编写一个简化的库,用于从vb.net应用程序读写excel 我只能在使用CreateWorkbook过程后写入excel文件。我无法使用AccessXLFile过程写入excel文件 有人能看一下吗 Imports Excel = Microsoft.Office.Interop.Excel 导入Microsoft.Office 公共类处理 Private xlApp As New Excel.Application Private xlWorkBook As Excel.Workb
Imports Excel = Microsoft.Office.Interop.Excel
导入Microsoft.Office
公共类处理
Private xlApp As New Excel.Application
Private xlWorkBook As Excel.Workbook = Nothing
Private xlWorkBooks As Excel.Workbooks = Nothing
Private xlWorkSheet As Excel.Worksheet = Nothing
Private Sub releaseObject(ByVal obj As Object) 'Closes an object using the garbage collector
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
Private Function PathExists(ByVal sPath As String) As Boolean 'Validate path
If My.Computer.FileSystem.DirectoryExists(sPath) Then
Return True
Else
Return False
End If
End Function
Public Function AccessXLFile(ByVal sFilePath As String) As Boolean 'Load an Excel file for reading & writing to it
Dim bRet As Boolean = True
Try
xlApp = New Excel.Application
xlApp.DisplayAlerts = False
xlWorkBooks = xlApp.Workbooks
xlWorkBook = xlWorkBooks.Open(sFilePath)
Catch ex As Exception
MsgBox("File not found, or Excel installation fault.")
bRet = False
End Try
Return bRet
End Function
Public Sub CloseXLFile() 'Clear memory
Try
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBooks)
releaseObject(xlWorkBook)
releaseObject(xlApp)
Catch ex As Exception
End Try
End Sub
Public Function SheetExists(sName As String) As Boolean 'File Exists check
Dim bResult As Boolean = False
For i = 1 To xlWorkBook.Worksheets.Count 'Scan sheet name through workbook
If xlWorkBook.Worksheets.Item(i).Name = sName Then
bResult = True
End If
Next i
Return bResult
End Function
Public Sub CreateWorkbook(ByVal sName As String, sPath As String, ByRef iDiag As Integer)
Dim misValue As Object = System.Reflection.Missing.Value
xlApp = New Excel.Application
If xlApp Is Nothing Then 'Error with excel installation on host PC
iDiag = 1
ElseIf (sName.Length = 0) Then 'Error with designed Workbook name
iDiag = 2
ElseIf (PathExists(sPath) = False) Then 'Error with pathname
iDiag = 3
Else
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
xlWorkSheet.Name = "Main"
xlWorkSheet = xlWorkBook.Sheets("Sheet2")
If Not xlWorkSheet Is Nothing Then
xlWorkSheet.Delete()
End If
xlWorkBook.SaveAs(sPath + sName + ".xlsx")
End If
releaseObject(xlApp)
End Sub
Public Sub CreateWorksheet(sSheetName As String, ByRef iDiag As Integer)
Dim misValue As Object = System.Reflection.Missing.Value
If sSheetName.Length = 0 Then 'Error sheet name null
iDiag = 1
ElseIf SheetExists(sSheetName)
iDiag = 2
Else
Dim xlNewSheet = DirectCast(xlWorkBook.Worksheets.Add(xlWorkBook.Worksheets(1), Type.Missing, Type.Missing, Type.Missing), Excel.Worksheet) 'Create new sheet
xlNewSheet.Name = sSheetName 'Rename new sheet
MsgBox(xlWorkBook.Worksheets.Count)
End If
End Sub
End Class找到了一个解决方案,即如果我使用AccessXLFile,为什么我不能使用我的CreateSheet过程。我修改了以下代码:
Public Sub CloseXLFile()'清除内存
尝试
xlWorkBook.Save()
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(XLWorkbook)
releaseObject(xlWorkBook)
releaseObject(xlApp)
特例
MsgBox(“?”)
结束尝试
末端接头
我添加了xlWorkBook.Save()我的意思是,在使用AccessXLFile函数之后,我不能使用CreateSheet过程