2017-07-18 11 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

)私はxlWorkBook.Saveを(追加

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

:私は、次のコードを修正しました – Pieterkii

答えて

0

私はAccessXLFileを使用した場合、私は私のCreateWorksheet手順を利用することができなかった理由解決策を見つけました。私はAccessXLFile機能を使用した後CreateWorksheet手順を使用することはできませんでした私は何を意味するのか

関連する問題