我正在尝试将两个大小相同的列表框的内容导出为Excel文件:两个标题,第一列-包含LB1-第二列-包含LB2。我试图为OpenXML提供代码,并设法保存了.xlsx文件,但是当我打开它时,Excel询问我是否要恢复该文件,如果这样,则文件从未成功恢复过。遵循我正在使用的代码:
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
Using spreadsheetDocument As SpreadsheetDocument = SpreadsheetDocument.Create(SaveFileDialog1.FileName, SpreadsheetDocumentType.Workbook, False)
Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart
workbookpart.Workbook = New Workbook
Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)()
worksheetPart.Worksheet = New Worksheet(New SheetData())
Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets())
Dim sheet As Sheet = New Sheet
sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart)
sheet.SheetId = 1
sheet.Name = "HRR"
sheets.Append(sheet)
Dim HeaderString As String = "Time [s], HRR [kW]"
Dim HeaderWords As String() = HeaderString.Split(New Char() {","c})
Dim Row = New Row()
For Each Header In HeaderWords
Dim cell = New Cell()
cell.DataType = CellValues.String
cell.CellValue = New CellValue(Trim(Header))
Row.AppendChild(cell)
Next
spreadsheetDocument.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(Row)
Dim DataString As String = "1,2"
Dim DataWords As String() = DataString.Split(New Char() {","c})
For Each DataPart In DataWords
Dim newRow = New Row()
For Each col As String In Form3.ListBox1.Items
Dim cell = New Cell()
cell.DataType = CellValues.String
cell.CellValue = New CellValue(DataPart.ToString())
newRow.AppendChild(cell)
Next
For Each col As String In Form3.ListBox2.Items
Dim cell = New Cell()
cell.DataType = CellValues.String
cell.CellValue = New CellValue(DataPart.ToString())
newRow.AppendChild(cell)
Next
spreadsheetDocument.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(newRow)
Next
workbookpart.Workbook.Save()
spreadsheetDocument.Close()
End Using
End If
你能告诉我我做错了什么吗?在此先感谢所有人都会回答我。最好的问候。