0

试图基于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
4

1 回答 1

0

您是否将任何数据填写到电子表格中?我生成的 excel 文件损坏的主要原因非常简单:excel 从 1 开始计数,而不是从 0 开始计数,记住这一点非常重要。

这是我的示例,但在 c#

    public void GetTemplate(Page page, DownloadExcelTemplateModel model)
    {
        // generating file
        byte[] bytes = GetFile(model);
        PutExcelFileToResponce(page, bytes);
    }

    private static void PutExcelFileToResponce(Page page, byte[] bytes)
    {
        // do clear
        page.Response.Clear();
        page.Response.ClearHeaders();
        page.Response.ClearContent();

        // add headers
        page.Response.AddHeader("Content-Length", bytes.Length.ToString(CultureInfo.InvariantCulture));
        page.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", "Template.xlsx"));
        page.Response.AddHeader("Content-Type", "application/Excel");

        // set content type
        page.Response.ContentType = "application/vnd.xls";

        // do write binary data to responce stream
        page.Response.BinaryWrite(bytes);

        // finish process
        page.Response.Flush();
        page.Response.End();
    }

    private byte[] GetFile(DownloadExcelTemplateModel model)
    {
        return WebService.GetData(model.Id);
    }
于 2012-10-31T16:31:38.297 回答