【问题标题】:Add Datatable to Existing Excel Spreadsheet vbnet, always Corrupt将数据表添加到现有的 Excel 电子表格 vbnet,总是损坏
【发布时间】:2018-02-08 17:59:51
【问题描述】:

更新: 似乎与 SHEETID 相关,现在它在使用 USING 标记时查找其他工作表,而不是与此 sheetid finder 组合。

    Dim sheetId As UInteger = 1
    If (sheets.Elements(Of Sheet).Count > 0) Then
        sheetId = CUInt(sheets.Elements(Of Sheet).Select(Function(s) s.SheetId.Value).Max + 1)
    End If

这是我用来使它工作的代码,现在停止损坏文件:

' Given a document name, inserts a new worksheet.
Public Sub InsertWorksheet(ByVal docName As String, ByVal SQL As DataTable, ByVal sheetName As String, ByVal intSheetId As Integer)
    'Dim sheetName As String
    Dim fileName As String = docName
    ' Open an existing spreadsheet document for editing.
    Dim spreadSheet As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, True)
    Using (spreadSheet)
        ' Add a blank WorksheetPart.
        Dim newWorksheetPart As WorksheetPart = spreadSheet.WorkbookPart.AddNewPart(Of WorksheetPart)()
        newWorksheetPart.Worksheet = New Worksheet(New SheetData())

        ' Create a Sheets object.
        Dim sheets As Sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild(Of Sheets)()
        Dim relationshipId As String = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart)

        ' Get a unique ID for the new worksheet.
        Dim sheetId As UInteger = 1
        If (sheets.Elements(Of Sheet).Count > 0) Then
            sheetId = CUInt(sheets.Elements(Of Sheet).Select(Function(s) s.SheetId.Value).Max + 1)
        End If

        ' Append the new worksheet and associate it with the workbook.
        Dim sheet As Sheet = New Sheet
        sheet.Id = relationshipId
        sheet.SheetId = sheetId
        sheet.Name = sheetName
        sheets.Append(sheet)

        'get the sheetData object so we can add the data table to it
        Dim sheetData As SheetData = newWorksheetPart.Worksheet.GetFirstChild(Of SheetData)()

        'add the data table
        AddDataTable(SQL, sheetData)

        'save the workbook
        newWorksheetPart.Worksheet.Save()

        ' Close the document.
        spreadSheet.Close()

    End Using

End Sub

文件在创建后总是损坏,尝试创建一个包含 4 个工作簿的电子表格,其中通过数据表加载了单独的数据。文件大小看起来是有效的,我在创建文件时没有收到任何特定错误。只是创建文件后不会打开excel表。

调用函数的现有代码:

    Try
        CreateExcelFileFromDataTable(iExcelFileLoc & ExportFileName, iAGetTable)
    Catch ex As Exception
        Dim ExceptionType As Integer = Type.GetTypeCode(ex.GetType())
        LogMessage(strAppName & "  - Failure : " & iExcelFileLoc & ExportFileName & "  Error:'" & ex.Message & "'  Error Type:'" & CStr(ExceptionType) & "'  Trace:" & ex.StackTrace, TraceEventType.Error)


    End Try

    Dim iBGetTable As DataTable = GetDataTable(SQL_SELECT_DOCUMENTS_TO_FOR_DOCS_NOT_FOUND_IN_DOCNUMS)
    Dim iReportB As String = BuildReportHTML(iBGetTable)
    InsertWorksheet(iExcelFileLoc & ExportFileName, iBGetTable, "Missing Scanned Documents", 2)

    ' ======================================================================

最初创建 Excel 的两个函数,然后是向现有电子表格添加带有数据表的新工作表的函数。


Public Sub InsertWorksheet(ByVal docName As String, ByVal SQL As DataTable, ByVal sheetName As String, ByVal intSheetId As Integer)

    Dim iFinalSheetName As String = ""



    Dim spreadSheet As SpreadsheetDocument = SpreadsheetDocument.Open(docName, True)


    Dim newWorksheetPart As WorksheetPart = spreadSheet.WorkbookPart.AddNewPart(Of WorksheetPart)()
    newWorksheetPart.Worksheet = New Worksheet(New SheetData())

    ' Add Sheets to the Workbook.
    Dim sheets As Sheets = spreadSheet.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets())
    Dim relationshipId As String = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart)
    ' Append a new worksheet and associate it with the workbook.

    Dim sheetId As UInteger = 1
    If (sheets.Elements(Of Sheet).Count > 0) Then
        sheetId = CUInt(sheets.Elements(Of Sheet).Select(Function(s) s.SheetId.Value).Max + 1)
    End If

    iFinalSheetName = (sheetName.ToString())

    ' Append the new worksheet and associate it with the workbook.
    Dim sheet As Sheet = New Sheet
    sheet.Id = relationshipId
    sheet.SheetId = CType(intSheetId, UInt32Value)
    sheet.Name = iFinalSheetName
    sheets.Append(sheet)

    'get the sheetData object so we can add the data table to it
    Dim sheetData As SheetData = newWorksheetPart.Worksheet.GetFirstChild(Of SheetData)()

    'add the data table
    AddDataTable(SQL, sheetData)

    'save the workbook
    newWorksheetPart.Worksheet.Save()

    ' Close the document.
    spreadSheet.Close()

End Sub


Public Sub CreateExcelFileFromDataTable(ByVal FilePath As String, myDT As DataTable)

    ' Create a spreadsheet document by supplying the filepath.
    ' By default, AutoSave = true, Editable = true, and Type = xlsx.
    Dim spreadsheetDocument As SpreadsheetDocument = spreadsheetDocument.Create(FilePath, SpreadsheetDocumentType.Workbook)

    ' Add a WorkbookPart to the document.
    Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart
    workbookpart.Workbook = New Workbook

    ' Add a WorksheetPart to the WorkbookPart.
    Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)()
    worksheetPart.Worksheet = New Worksheet(New SheetData())

    ' Add Sheets to the Workbook.
    Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets())

    ' Append a new worksheet and associate it with the workbook.
    Dim sheet As Sheet = New Sheet
    sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart)
    sheet.SheetId = 1
    sheet.Name = "Duplicate Document"

    sheets.Append(sheet)

    'get the sheetData object so we can add the data table to it
    Dim sheetData As SheetData = worksheetPart.Worksheet.GetFirstChild(Of SheetData)()

    'add the data table
    'AddDataTable(myDT, sheetData)

    'save the workbook
    workbookpart.Workbook.Save()

    ' Close the document.
    spreadsheetDocument.Close()

    ' -----------------------------------

End Sub

【问题讨论】:

    标签: excel vb.net datatable spreadsheet


    【解决方案1】:

    这里有一个选择:

    1. 手动创建电子表格示例。
    2. 下载并安装 Open XML SDK 工具
    3. 在工具中打开电子表格
    4. 打开电子表格后,您可以在其中查看 .NET 源代码以创建与其相似的工作表。

    我经常使用这个工具。在没有它的情况下使用 Office XML SDK 会非常麻烦。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      相关资源
      最近更新 更多