I'm creating an Excel file with multiple sheets using DocumentFormat.OpenXml
library from my C#
code. The file gets created without any issue and it has all the required data but when I open it, I get this error pop-up:
I have noticed that this erroneous file has a type of XSLX file
. If it is of the type Microsoft Excel Worksheet
, strangely, it doesn't throw any error:
I'm just not getting what is wrong here an how I can fix it. Need suggestions, please. Below is my code snippet:
private static void WriteToExcelFile(HashSet<string> modules, Dictionary<string, HashSet<MyData>> dataDictionary)
{
if (!Directory.Exists(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Content\\MyDocument")))
Directory.CreateDirectory(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Content\\MyDocument"));
string fileFullname = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Content\\MyDocument", "MyExcel.xslx");
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(fileFullname, SpreadsheetDocumentType.Workbook);
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart(); // Add a WorkbookPart to the document
workbookpart.Workbook = new Workbook();
// Add Sheets to the Workbook
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(new Sheets());
// populate sheets
.....
spreadsheetDocument.Close();
}
CodePudding user response:
The error comes from a wrong extension: it MUST be .xlsx And not .xslx