C# Excel文件在下载时会损坏

C# Excel文件在下载时会损坏,c#,excel,.net-core,C#,Excel,.net Core,我正在使用库在Asp.NETCORE2.2中创建excell文件。问题是,当我创建一个Api来下载创建的Excell文件时,它会损坏,当我尝试用MS Excell打开它时,它会说文件格式和文件扩展名不匹配。以下是我创建Excell和Api的代码: [HttpGet] public IActionResult Excel () { using (ExcelEngine excelEngine = new ExcelEngine ()) {

我正在使用库在Asp.NETCORE2.2中创建excell文件。问题是,当我创建一个Api来下载创建的Excell文件时,它会损坏,当我尝试用MS Excell打开它时,它会说文件格式和文件扩展名不匹配。以下是我创建Excell和Api的代码:

 [HttpGet] public IActionResult Excel () {
            using (ExcelEngine excelEngine = new ExcelEngine ()) {
                //Instantiate the Excel application object
                IApplication application = excelEngine.Excel;

                //Assigns default application version
                application.DefaultVersion = ExcelVersion.Excel2013;

                //A new workbook is created equivalent to creating a new workbook in Excel
                //Create a workbook with 1 worksheet
                IWorkbook workbook = application.Workbooks.Create (1);

                //Access a worksheet from workbook
                IWorksheet worksheet = workbook.Worksheets[0];

                //Adding text data
                worksheet.Range["A1"].Text = "Month";
                worksheet.Range["B1"].Text = "Sales";
                worksheet.Range["A6"].Text = "Total";

                //Adding DateTime data
                worksheet.Range["A2"].DateTime = new DateTime (2015, 1, 10);
                worksheet.Range["A3"].DateTime = new DateTime (2015, 2, 10);
                worksheet.Range["A4"].DateTime = new DateTime (2015, 3, 10);

                //Applying number format for date value cells A2 to A4
                worksheet.Range["A2:A4"].NumberFormat = "mmmm, yyyy";

                //Auto-size the first column to fit the content
                worksheet.AutofitColumn (1);

                //Adding numeric data
                worksheet.Range["B2"].Number = 68878;
                worksheet.Range["B3"].Number = 71550;
                worksheet.Range["B4"].Number = 72808;

                //Adding formula
                worksheet.Range["B6"].Formula = "SUM(B2:B4)";
                var name = Guid.NewGuid() + ".xlsx";

                //Inserting image

                //Saving the workbook to disk in XLSX format
                FileStream fileStream = new FileStream (name, FileMode.Create, FileAccess.ReadWrite); // Copy file stream to MemoryStream.
                MemoryStream memoryStream = new MemoryStream ();
                fileStream.CopyTo (memoryStream);
                // Gets byte array from memory stream of file.
                byte[] temp = memoryStream.ToArray ();
                excelEngine.Dispose();
                return File (temp, "application/ms-excel", name);
            }


        }

由于他人的善意帮助,最终能够正常工作的代码:

[HttpPost] public IActionResult Excel () {
            using (ExcelEngine excelEngine = new ExcelEngine ()) {
                //Instantiate the Excel application object
                IApplication application = excelEngine.Excel;

                //Assigns default application version
                application.DefaultVersion = ExcelVersion.Excel2013;

                //A new workbook is created equivalent to creating a new workbook in Excel
                //Create a workbook with 1 worksheet
                IWorkbook workbook = application.Workbooks.Create (1);

                //Access a worksheet from workbook
                IWorksheet worksheet = workbook.Worksheets[0];

                //Adding text data
                worksheet.Range["A1"].Text = "Month";
                worksheet.Range["B1"].Text = "Sales";
                worksheet.Range["A6"].Text = "Total";

                //Adding DateTime data
                worksheet.Range["A2"].DateTime = new DateTime (2015, 1, 10);
                worksheet.Range["A3"].DateTime = new DateTime (2015, 2, 10);
                worksheet.Range["A4"].DateTime = new DateTime (2015, 3, 10);

                //Applying number format for date value cells A2 to A4
                worksheet.Range["A2:A4"].NumberFormat = "mmmm, yyyy";

                //Auto-size the first column to fit the content
                worksheet.AutofitColumn (1);

                //Adding numeric data
                worksheet.Range["B2"].Number = 68878;
                worksheet.Range["B3"].Number = 71550;
                worksheet.Range["B4"].Number = 72808;

                //Adding formula
                worksheet.Range["B6"].Formula = "SUM(B2:B4)";
                var name = Guid.NewGuid () + ".xlsx";
                // FileStream inputStream = new FileStream (name, FileMode.Create, FileAccess.ReadWrite);
                string ContentType = "Application/msexcel";
                MemoryStream outputStream = new MemoryStream ();
                workbook.SaveAs (outputStream);
                outputStream.Position = 0;
                return File (outputStream, ContentType, name);
            }

        }

创建工作簿后,将其保存在何处?我所能看到的是,您稍后创建一个空文件,然后将其复制到内存流并返回内容——我希望是空的。正如Charles所说,您不会将Excel文件保存在任何地方。此外,我不确定您是否想要
FileMode.Create
,即使该文件确实存在。感谢您的回复。如何将工作簿保存在FileStream中?我建议您查看一下。