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 に答える