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?
- public static string OpenXML(string FolderPath, DataSet tableSet)
- {
- WorkbookPart wBookPart = null;
- var datetime = DateTime.Now.ToString().Replace("/", "_").Replace(":", "_");
- string FilePath = "";
- foreach (DataTable table1 in tableSet.Tables)
- {
- if (table1.Rows.Count != 0)
- {
- using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Create(FilePath, SpreadsheetDocumentType.Workbook))
- {
- wBookPart = spreadsheetDoc.AddWorkbookPart();
- wBookPart.Workbook = new Workbook();
- uint sheetId = 1;
- spreadsheetDoc.WorkbookPart.Workbook.Sheets = new Sheets();
- Sheets sheets = spreadsheetDoc.WorkbookPart.Workbook.GetFirstChild();
- WorkbookStylesPart wbsp = wBookPart.AddNewPart();
- wbsp.Stylesheet = CreateStylesheet();
- wbsp.Stylesheet.Save();
- foreach (DataTable table in tableSet.Tables)
- {
- if (table.Rows.Count != 0)
- {
- table.TableName = table.Rows[0]["LeaseCondition"].ToString();
- WorksheetPart wSheetPart = wBookPart.AddNewPart();
- Sheet sheet = new Sheet() { Id = spreadsheetDoc.WorkbookPart.GetIdOfPart(wSheetPart), SheetId = sheetId, Name = table.TableName };
- sheets.Append(sheet);
- SheetData sheetData = new SheetData();
- wSheetPart.Worksheet = new Worksheet();
- Row headerRow = new Row();
- Columns columns = new Columns();
- int ColumnNumber = 1;
- foreach (DataColumn column in table.Columns)
- {
- Cell cell = new Cell();
- cell.DataType = CellValues.String;
- cell.CellValue = new CellValue(column.ColumnName);
- cell.StyleIndex = 2;
- headerRow.AppendChild(cell);
- Column column1 = new Column();
- column1.Width = 30; ;
- column1.BestFit = true;
- column1.CustomWidth = true;
- column1.Min = Convert.ToUInt32(ColumnNumber);
- column1.Max = Convert.ToUInt32(ColumnNumber);
- columns.AppendChild(column1);
- ColumnNumber = ColumnNumber + 1;
- }
- wSheetPart.Worksheet.AppendChild(columns);
- sheetData.AppendChild(headerRow);
- foreach (DataRow dr in table.Rows)
- {
- Row row = new Row();
- foreach (DataColumn column in table.Columns)
- {
- Cell cell = new Cell();
- cell.DataType = CellValues.String;
- cell.CellValue = new CellValue(dr[column].ToString());
- cell.StyleIndex = 1;
- row.AppendChild(cell);
- }
- sheetData.AppendChild(row);
- }
- sheetId++;
- wSheetPart.Worksheet.AppendChild(sheetData);
- }
- }
- }
- }
- }
- return FilePath;
- }