I am getting error 'Excel found unreadable content in APPL_1.xlsx.' What is wrong in my code?.
Record contain around 2 lacks data. I am trying to fetch it from datatable to excel.
I am using OpenXMLSpreedsheetDocument to fetch data from datatable to excel.
Any suggestions is much appreciated..!!
- public void ExportDataTable_SpreadsheetDocument(DataTable dt, string FilePath)
- {
- try
- {
- FileInfo FileLoc = new FileInfo(FilePath);
- if (FileLoc.Exists)
- {
- FileLoc.Delete();
- FileLoc = new FileInfo(FilePath);
- }
- SpreadsheetDocument spreadSheet = SpreadsheetDocument.
- Create(FilePath, SpreadsheetDocumentType.Workbook);
-
- WorkbookPart workbookpart = spreadSheet.AddWorkbookPart();
- workbookpart.Workbook = new Workbook();
-
-
- var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
- var sheetData = new SheetData();
- worksheetPart.Worksheet = new Worksheet(sheetData);
- var bold1 = new Bold();
- CellFormat cf = new CellFormat();
-
- Sheets sheets;
- sheets = spreadSheet.WorkbookPart.Workbook.
- AppendChild<Sheets>(new Sheets());
-
-
- var sheet = new Sheet()
- {
- Id = spreadSheet.WorkbookPart.
- GetIdOfPart(worksheetPart),
- SheetId = 0,
- Name = "Sheet" + 0
- };
- sheets.Append(sheet);
-
- var headerRow = new Row();
- foreach (DataColumn column in dt.Columns)
- {
- var cell = new Cell
- {
- DataType = CellValues.String,
- CellValue = new CellValue(column.ColumnName)
- };
- headerRow.AppendChild(cell);
- }
- sheetData.AppendChild(headerRow);
- foreach (DataRow row in dt.Rows)
- {
- var newRow = new Row();
- foreach (DataColumn col in dt.Columns)
- {
- Cell c = new Cell();
- c.DataType = new EnumValue<CellValues>(CellValues.String);
- c.CellValue = new CellValue(row[col].ToString());
- newRow.Append(c);
- }
- sheetData.AppendChild(newRow);
- }
- workbookpart.Workbook.Save();
- ProcessStartInfo startInfo = new ProcessStartInfo(FilePath);
- Process.Start(startInfo);
- }
- catch (Exception ex)
- {
- }
- }