簡體   English   中英

VB.net Excel對象寫入它

[英]VB.net Excel object writing to it

我正在嘗試編寫一個簡化的庫,以便從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

最終班

找到了解決方案,如果我使用AccessXLFile,為什么我無法利用我的CreateWorksheet過程。 我修改了以下代碼:

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM