Solved

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

Posted on 2014-09-30
4
680 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

RoboForm Secure Password Management System

RoboForm Everywhere - Superb Browser Support
Windows / Apple / IOS / Android / Linux / Chrome OS
Use different complex passwords everywhere
Best Secure Password Management by far
Synchronize all of your devices instantly
Safe, Secure & Highly Recommended!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

734 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