2017-07-18 65 views
0

我想寫一個簡化的庫讀取&從vb.net應用程序寫入excel。VB.net Excel對象寫入

我只能在使用我的CreatewWorkbook過程後寫入excel文件。我無法使用我的AccessXLFile過程寫入Excel文件。

有人可以看看嗎?

Imports Excel = Microsoft.Office.Interop.Excel 

進口Microsoft.Office

公共類xlHandling

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 

末級

+0

我的意思是,我不能用使用AccessXLFile功能後CreateWorksheet程序 – Pieterkii

回答

0

找到了解決辦法,爲什麼我不能讓我的應用程序CreateWorksheet如果我用AccessXLFile。我修改了下面的代碼:

Public Sub CloseXLFile() 'Clear memory Try xlWorkBook.Save() xlWorkBook.Close() xlApp.Quit() releaseObject(xlWorkSheet) releaseObject(xlWorkBooks) releaseObject(xlWorkBook) releaseObject(xlApp) Catch ex As Exception MsgBox("?") End Try End Sub

我增加了xlWorkBook.Save()