试图基于http://www.codeproject.com/Articles/371203/Creating-basic-Excel-workbook-with-Open-XML创建一个简单的 OpenXML 文档, 我将它移到我的 asp.net(下) dev 服务器,但创建的文件已损坏且无法打开。我在做一些明显愚蠢的事情吗?
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Response.Clear()
Response.Buffer = False
Dim myoutputstream As New MemoryStream
Dim myPackage As SpreadsheetDocument = CreateWorkbook(myoutputstream)
AddWorksheet(myPackage, "Test")
myPackage.WorkbookPart.Workbook.Save()
Response.AddHeader("Content-Disposition", ("attachment") & "; filename=Report.xlsx")
Response.ContentType = "application/octet-stream"
Response.AddHeader("Accept-Header", myoutputstream.Length.ToString())
Response.AddHeader("Content-Length", myoutputstream.Length.ToString())
Response.BinaryWrite(myoutputstream.GetBuffer())
Response.End()
End Sub
Public Shared Function CreateWorkbook(ByVal MyMemoryStream As MemoryStream) As SpreadsheetDocument
Dim spreadSheet As SpreadsheetDocument = Nothing
Dim sharedStringTablePart As SharedStringTablePart
Dim workbookStylesPart As WorkbookStylesPart
Try
' Create the Excel workbook
spreadSheet = SpreadsheetDocument.Create(MyMemoryStream, SpreadsheetDocumentType.Workbook, False)
' Create the parts and the corresponding objects
' Workbook
spreadSheet.AddWorkbookPart()
spreadSheet.WorkbookPart.Workbook = New DocumentFormat.OpenXml.Spreadsheet.Workbook()
spreadSheet.WorkbookPart.Workbook.Save()
' Shared string table
sharedStringTablePart = spreadSheet.WorkbookPart.AddNewPart(Of SharedStringTablePart)()
sharedStringTablePart.SharedStringTable = New DocumentFormat.OpenXml.Spreadsheet.SharedStringTable()
sharedStringTablePart.SharedStringTable.Save()
' Sheets collection
spreadSheet.WorkbookPart.Workbook.Sheets = New DocumentFormat.OpenXml.Spreadsheet.Sheets()
spreadSheet.WorkbookPart.Workbook.Save()
' Stylesheet
workbookStylesPart = spreadSheet.WorkbookPart.AddNewPart(Of WorkbookStylesPart)()
workbookStylesPart.Stylesheet = New DocumentFormat.OpenXml.Spreadsheet.Stylesheet()
workbookStylesPart.Stylesheet.Save()
Catch exception As System.Exception
End Try
Return spreadSheet
End Function
Public Shared Function AddWorksheet(ByVal spreadsheet As SpreadsheetDocument, ByVal name As String) As Boolean
Dim sheets As DocumentFormat.OpenXml.Spreadsheet.Sheets = spreadsheet.WorkbookPart.Workbook.GetFirstChild(Of DocumentFormat.OpenXml.Spreadsheet.Sheets)()
Dim sheet As DocumentFormat.OpenXml.Spreadsheet.Sheet
Dim worksheetPart As WorksheetPart
' Add the worksheetpart
worksheetPart = spreadsheet.WorkbookPart.AddNewPart(Of WorksheetPart)()
worksheetPart.Worksheet = New DocumentFormat.OpenXml.Spreadsheet.Worksheet(New DocumentFormat.OpenXml.Spreadsheet.SheetData())
worksheetPart.Worksheet.Save()
' Add the sheet and make relation to workbook
sheet = New DocumentFormat.OpenXml.Spreadsheet.Sheet With {
.Id = spreadsheet.WorkbookPart.GetIdOfPart(worksheetPart),
.SheetId = (spreadsheet.WorkbookPart.Workbook.Sheets.Count() + 1),
.Name = name}
sheets.Append(sheet)
spreadsheet.WorkbookPart.Workbook.Save()
Return True
End Function