Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8765029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:11:08+00:00 2026-06-13T16:11:08+00:00

trying to just create a simple OpenXML document based on http://www.codeproject.com/Articles/371203/Creating-basic-Excel-workbook-with-Open-XML i moved it

  • 0

trying to just create a simple OpenXML document based on http://www.codeproject.com/Articles/371203/Creating-basic-Excel-workbook-with-Open-XML
i moved it over to asp.net (below) on my dev server but the file that is created is corrupted and will not open. am i doing something obviously stupid?

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:11:09+00:00Added an answer on June 13, 2026 at 4:11 pm

    Do you fill any data into spreadsheet?
    My main reason why my generated excel files gets corrupted is really simple : excel start count from 1 but not from 0, and this is really important to remember.

    Here is my example, but on 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);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a very simple REST server. I just have a test
This is just a simple question. I was trying to create a new object
I am trying to just create a basic layout, but i am having trouble
Just trying to create a simple method that counts up 1 on the tally
I'm trying to create simple DER decoder - console application that just outputs content
Just for the kicks i am trying to create a simple data object in
I have just gotten started with the Yii framework, trying to create a simple
I am just trying to create the XML use XML::Simple; my %element = (
I have just started learning Qt and I am trying to create a simple
I'm just trying to create a new MySQL database with a few simple tables.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.