Solved

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

Posted on 2014-09-30
4
642 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you how to use shortcut menus in the Access run-time environment.
My experience with Windows 10 over a one year period and suggestions for smooth operation
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

809 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