更新: 似乎与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
答案 0 :(得分:0)
以下是您的选择:
我经常使用这个工具。在没有它的情况下使用Office XML SDK可能非常麻烦。