Excel 通过代码 C# 创建时损坏

Excel getting corrupt when created through Code C#

我正在尝试使用内存流创建 excel。这是我正在尝试的代码,

        using (MemoryStream mem = new MemoryStream())
        {
            using (var sheetDocument = SpreadsheetDocument.Create(mem, SpreadsheetDocumentType.Workbook))
            {
                //// Add a WorkbookPart to the document.
                WorkbookPart workbookpart = sheetDocument.AddWorkbookPart();
                workbookpart.Workbook = new Workbook();

                WorkbookStylesPart workbookstyle = workbookpart.AddNewPart<WorkbookStylesPart>();

                workbookstyle.Stylesheet = this.GenerateStyleSheet();
                workbookstyle.Stylesheet.Save();

                //// Add a WorksheetPart to the WorkbookPart.
                WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
                worksheetPart.Worksheet = new Worksheet();

                Columns cols = new Columns();
                Column col = new Column();
                col.BestFit = true;
                col.CustomWidth = true;
                col.Width = 22.33;
                col.Max = 13;
                col.Min = 1;
                cols.Append(col);

                worksheetPart.Worksheet.Append(cols);

                SheetData sheetData = new SheetData();
                worksheetPart.Worksheet.Append(sheetData);

                //// Add Sheets to the Workbook.
                Sheets sheets = sheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

                //// Append a new worksheet and associate it with the workbook.
                Sheet sheet = new Sheet() { Id = sheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = sheetName };
                sheets.Append(sheet);

                workbookpart.Workbook.Save();

                List<string> columns = new List<string>();

                foreach (DataColumn column in table.Columns)
                {
                    columns.Add(column.ColumnName);
                }

                DocumentFormat.OpenXml.Spreadsheet.Row headerRow = this.CreateHeaderRow(columns);
                sheetData.AppendChild(headerRow);

                foreach (DataRow dataSetRow in table.Rows)
                {
                    DocumentFormat.OpenXml.Spreadsheet.Row newRow = this.CreateDataRow(dataSetRow);
                    sheetData.AppendChild(newRow);
                }

                workbookpart.Workbook.Save();
                var result = mem.ToArray();
                System.IO.File.WriteAllBytes(fileName, result);
            }

正在创建一个 excel 文件,但是当我尝试打开它时,出现如下错误

如果我按是,它会说

The workbook cannot be opened or repaired by Microsoft Excel because it is corrupt

我该如何解决这个问题?

感谢@zaikhan 的评论,我得以解决我的问题。当我关闭 sheetDocument 时它起作用了。

using (MemoryStream mem = new MemoryStream())
        {
            using (var sheetDocument = SpreadsheetDocument.Create(mem, SpreadsheetDocumentType.Workbook))
            {
                //// Add a WorkbookPart to the document.
                WorkbookPart workbookpart = sheetDocument.AddWorkbookPart();
                workbookpart.Workbook = new Workbook();

                WorkbookStylesPart workbookstyle = workbookpart.AddNewPart<WorkbookStylesPart>();

                workbookstyle.Stylesheet = this.GenerateStyleSheet();
                workbookstyle.Stylesheet.Save();

                //// Add a WorksheetPart to the WorkbookPart.
                WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
                worksheetPart.Worksheet = new Worksheet();

                Columns cols = new Columns();
                Column col = new Column();
                col.BestFit = true;
                col.CustomWidth = true;
                col.Width = 22.33;
                col.Max = 13;
                col.Min = 1;
                cols.Append(col);

                worksheetPart.Worksheet.Append(cols);

                SheetData sheetData = new SheetData();
                worksheetPart.Worksheet.Append(sheetData);

                //// Add Sheets to the Workbook.
                Sheets sheets = sheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

                //// Append a new worksheet and associate it with the workbook.
                Sheet sheet = new Sheet() { Id = sheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = sheetName };
                sheets.Append(sheet);

                workbookpart.Workbook.Save();

                List<string> columns = new List<string>();

                foreach (DataColumn column in table.Columns)
                {
                    columns.Add(column.ColumnName);
                }

                DocumentFormat.OpenXml.Spreadsheet.Row headerRow = this.CreateHeaderRow(columns);
                sheetData.AppendChild(headerRow);

                foreach (DataRow dataSetRow in table.Rows)
                {
                    DocumentFormat.OpenXml.Spreadsheet.Row newRow = this.CreateDataRow(dataSetRow);
                    sheetData.AppendChild(newRow);
                }

                workbookpart.Workbook.Save();
                sheetDocument.Close();
            }

            var result = mem.ToArray();
            System.IO.File.WriteAllBytes(fileName, result);
          }