Solved

Unable to create Excel file in vb.net using Open XML / OpenDocument SDK

Posted on 2014-09-30
4
623 Views
Last Modified: 2014-10-10
I'm struggling with a  vb.Net project to create a simple Excel file using Open XML.  I have installed Open XML 2.5 and have added WindowsBase 4.0.0.0 and DocumentFormat.OpenXml 2.5.5631.0 to the project and the code below compiles fine.

The problem is the resulting file is corrupt.  I would upload it but Experts Exchange is reporting an error when I try.

Is anything obviously wrong with the code below?  I have found posts referencing a OpenDocument Tool I should be able to use to view the file but I don't seem to have it on my system and cannot find where to download it.

        Dim excelStream As New MemoryStream()

        Using spreadSheet = SpreadsheetDocument.Create(excelStream, SpreadsheetDocumentType.Workbook)
            'Create workbook
            spreadSheet.AddWorkbookPart()
            spreadSheet.WorkbookPart.Workbook = New Workbook()

            'Create worksheet
            spreadSheet.WorkbookPart.AddNewPart(Of WorksheetPart)()
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet = New Worksheet()

            'Create worksheet
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.AppendChild(New SheetData())

            'Create Header Row
            Dim row = spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(New Row())
            row.Append(New Cell With {.DataType = CellValues.String, .CellValue = New CellValue("TestHeader")})

            'Save worksheet
            spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.Save()

            'Create the worksheet to workbook relation
            spreadSheet.WorkbookPart.Workbook.AppendChild(New Sheets())
            spreadSheet.WorkbookPart.Workbook.GetFirstChild(Of Sheets).AppendChild(
                New Sheet() With
                {
                    .Id = spreadSheet.WorkbookPart.GetIdOfPart(spreadSheet.WorkbookPart.WorksheetParts.First()),
                    .SheetId = 1,
                    .Name = "StockNbr"
                }
            )

            'Save and reset stream position
            spreadSheet.WorkbookPart.Workbook.Save()
        End Using

        excelStream.Position = 0
        Dim fileOut As New FileStream("C:\test.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite)
        excelStream.CopyTo(fileOut)
        fileOut.Flush()
        fileOut.Close()

Open in new window

0
Comment
Question by:canuckconsulting
  • 2
  • 2
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 40355061
When you try to open the file with Excel, is there an option to view the log?  In the past, I have seen a log that tries to show the issues with opening the file.
0
 

Author Comment

by:canuckconsulting
ID: 40361255
No, I don't get any option.  I'm just advised that the file is corrupt.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40361552
I am thinking that it you shouldn't write to an .xlsx file since that is a specialize compressed package file structure, and Excel would expect that structure.
0
 

Author Closing Comment

by:canuckconsulting
ID: 40373172
It ended up being something local on my machine as compiling the same code and running it on another box worked fine.  I'm marking this as the answer to close the question though in my case it wasn't the issue.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now