Soumyadeeep Raha

Soumyadeeep Raha

  • NA
  • 18
  • 1.1k

Excel Open error: “found unreadable content in APPL_1.xlsx”

Dec 24 2018 5:17 AM
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..!!
  1. public void ExportDataTable_SpreadsheetDocument(DataTable dt, string FilePath)  
  2. {  
  3. try  
  4. {  
  5. FileInfo FileLoc = new FileInfo(FilePath);  
  6. if (FileLoc.Exists)  
  7. {  
  8. FileLoc.Delete();  
  9. FileLoc = new FileInfo(FilePath);  
  10. }  
  11. SpreadsheetDocument spreadSheet = SpreadsheetDocument.  
  12. Create(FilePath, SpreadsheetDocumentType.Workbook);  
  13. // Add a WorkbookPart to the document.  
  14. WorkbookPart workbookpart = spreadSheet.AddWorkbookPart();  
  15. workbookpart.Workbook = new Workbook();  
  16.   
  17. // Add a WorksheetPart to the WorkbookPart.  
  18. var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();  
  19. var sheetData = new SheetData();  
  20. worksheetPart.Worksheet = new Worksheet(sheetData);  
  21. var bold1 = new Bold();  
  22. CellFormat cf = new CellFormat();  
  23. // Add Sheets to the Workbook.  
  24. Sheets sheets;  
  25. sheets = spreadSheet.WorkbookPart.Workbook.  
  26. AppendChild<Sheets>(new Sheets());  
  27.   
  28. // Append a new worksheet and associate it with the workbook.  
  29. var sheet = new Sheet()  
  30. {  
  31. Id = spreadSheet.WorkbookPart.  
  32. GetIdOfPart(worksheetPart),  
  33. SheetId = 0,  
  34. Name = "Sheet" + 0  
  35. };  
  36. sheets.Append(sheet);  
  37. //Add Header Row.  
  38. var headerRow = new Row();  
  39. foreach (DataColumn column in dt.Columns)  
  40. {  
  41. var cell = new Cell  
  42. {  
  43. DataType = CellValues.String,  
  44. CellValue = new CellValue(column.ColumnName)  
  45. };  
  46. headerRow.AppendChild(cell);  
  47. }  
  48. sheetData.AppendChild(headerRow);  
  49. foreach (DataRow row in dt.Rows)  
  50. {  
  51. var newRow = new Row();  
  52. foreach (DataColumn col in dt.Columns)  
  53. {  
  54. Cell c = new Cell();  
  55. c.DataType = new EnumValue<CellValues>(CellValues.String);  
  56. c.CellValue = new CellValue(row[col].ToString());  
  57. newRow.Append(c);  
  58. }  
  59. sheetData.AppendChild(newRow);  
  60. }  
  61. workbookpart.Workbook.Save();  
  62. ProcessStartInfo startInfo = new ProcessStartInfo(FilePath);  
  63. Process.Start(startInfo);  
  64. }  
  65. catch (Exception ex)  
  66. {  
  67. }  
  68. }