Quantcast
Channel: Excel Data Reader - Read Excel files in .NET
Viewing all articles
Browse latest Browse all 448

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

$
0
0
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.

Viewing all articles
Browse latest Browse all 448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>