Sujeet Raman

Sujeet Raman

  • 864
  • 927
  • 352.9k

Opening excel file gives message box “content recovery of the work

Sep 16 2020 4:44 AM
While I'm trying to open excel file a message box is prompting like "We found a problem with some content in file name. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.". I don't understand why this happened and none of the internet solution is not working for me. Can anyone help me?
  1. public static string OpenXML(string FolderPath, DataSet tableSet)  
  2. {  
  3. WorkbookPart wBookPart = null;  
  4. var datetime = DateTime.Now.ToString().Replace("/""_").Replace(":""_");  
  5. string FilePath = "";  
  6. foreach (DataTable table1 in tableSet.Tables)  
  7. {  
  8. if (table1.Rows.Count != 0)  
  9. {  
  10. using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Create(FilePath, SpreadsheetDocumentType.Workbook))  
  11. {  
  12. wBookPart = spreadsheetDoc.AddWorkbookPart();  
  13. wBookPart.Workbook = new Workbook();  
  14. uint sheetId = 1;  
  15. spreadsheetDoc.WorkbookPart.Workbook.Sheets = new Sheets();  
  16. Sheets sheets = spreadsheetDoc.WorkbookPart.Workbook.GetFirstChild();  
  17. WorkbookStylesPart wbsp = wBookPart.AddNewPart();  
  18. wbsp.Stylesheet = CreateStylesheet();  
  19. wbsp.Stylesheet.Save();  
  20. foreach (DataTable table in tableSet.Tables)  
  21. {  
  22. if (table.Rows.Count != 0)  
  23. {  
  24. table.TableName = table.Rows[0]["LeaseCondition"].ToString();  
  25. WorksheetPart wSheetPart = wBookPart.AddNewPart();  
  26. Sheet sheet = new Sheet() { Id = spreadsheetDoc.WorkbookPart.GetIdOfPart(wSheetPart), SheetId = sheetId, Name = table.TableName };  
  27. sheets.Append(sheet);  
  28. SheetData sheetData = new SheetData();  
  29. wSheetPart.Worksheet = new Worksheet();  
  30. Row headerRow = new Row();  
  31. Columns columns = new Columns();  
  32. int ColumnNumber = 1;  
  33. foreach (DataColumn column in table.Columns)  
  34. {  
  35. Cell cell = new Cell();  
  36. cell.DataType = CellValues.String;  
  37. cell.CellValue = new CellValue(column.ColumnName);  
  38. cell.StyleIndex = 2;  
  39. headerRow.AppendChild(cell);  
  40. Column column1 = new Column();  
  41. column1.Width = 30; ;  
  42. column1.BestFit = true;  
  43. column1.CustomWidth = true;  
  44. column1.Min = Convert.ToUInt32(ColumnNumber);  
  45. column1.Max = Convert.ToUInt32(ColumnNumber);  
  46. columns.AppendChild(column1);  
  47. ColumnNumber = ColumnNumber + 1;  
  48. }  
  49. wSheetPart.Worksheet.AppendChild(columns);  
  50. sheetData.AppendChild(headerRow);  
  51. foreach (DataRow dr in table.Rows)  
  52. {  
  53. Row row = new Row();  
  54. foreach (DataColumn column in table.Columns)  
  55. {  
  56. Cell cell = new Cell();  
  57. cell.DataType = CellValues.String;  
  58. cell.CellValue = new CellValue(dr[column].ToString());  
  59. cell.StyleIndex = 1;  
  60. row.AppendChild(cell);  
  61. }  
  62. sheetData.AppendChild(row);  
  63. }  
  64. sheetId++;  
  65. wSheetPart.Worksheet.AppendChild(sheetData);  
  66. }  
  67. }  
  68. }  
  69. }  
  70. }  
  71. return FilePath;  
  72. }  

Answers (1)