文件格式和扩展名不匹配。该文件可能已损坏或不安全

The file format and extension of don't match. The file could be corrupted or unsafe

我正在使用 C# 代码中的 DocumentFormat.OpenXml 库创建一个包含多个工作表的 Excel 文件。该文件的创建没有任何问题,它包含所有必需的数据,但是当我打开它时,我收到此错误弹出窗口:

我注意到这个错误文件的类型为 XSLX file。如果它是 Microsoft Excel Worksheet 类型,奇怪的是,它不会抛出任何错误:

我只是不知道这里出了什么问题以及如何解决它。需要建议,请。 下面是我的代码片段:

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();
}

错误来自错误的扩展名:它必须是 .xlsx 并不是 .xslx