After Saving excel document with Open XML the file does not want to open.

Dec 19, 2013 at 11:09 AM
Hi

I am just doing a basic save from the Open XML library, so no changes is being done to the excel file. But when I try to read it with the Excel Data Reader it gives my sheet count back as 0. I compared the 2 files in their xml form and they are identical.

Saving the file:
                    WorksheetPart wsPart = wbPart.GetPartById(s.Id) as WorksheetPart;
                    WorksheetWriter writer = new WorksheetWriter(doc, wsPart);

                    if (wsPart == null)
                    {
                        continue;
                    }
var calcChainPart = wbPart.CalculationChainPart;
                    wbPart.DeletePart(calcChainPart);
                    wbPart.DeletePart(wbPart.VbaProjectPart);

                    foreach (Row row in wsPart.Worksheet.Descendants<Row>())
                    {
                        foreach (Cell c in row.Elements<Cell>())
                        {
                           
                            if (c.CellFormula != null && (!(String.IsNullOrEmpty(c.CellFormula.ToString()))))
                            {
                                c.CellFormula.Remove();
                            }


                        }
                    } 
                }
                wbPart.Workbook.Save();
Opening again in Excel Reader:
FileStream stream = System.IO.File.Open(@"D:\Book1.xlsx", FileMode.Open);

                        IExcelDataReader excelReader;
                        switch (ext)
                        {
                            case ".xls":
                                excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                                break;
                            case ".xlsx":
                                excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                                break;
                            case ".xlsm":
                                excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                                break;
                            default:
                                ErrorMessage = "Incompatible file type.";
                                return RedirectToAction("Index");
                        }
                        
                        DataSet result = excelReader.AsDataSet();
                        int sheetCount = result.Tables.Count;
When opening the file with excel it has no problems.
Developer
Dec 19, 2013 at 11:59 AM
I'm not sure if there are any test cases for Open XML at the moment. There must be some difference in the xml though, otherwise it would work.
Dec 19, 2013 at 12:41 PM
I see there is minor differences, openxml has changed one of the namespaces which maybe could have caused this.
I think I will close the issue and use linq to excel which currently works.