excel文件下载后会损坏



我正在使用SyncFusion库在ASP.NET Core2.2中创建Excell File。问题在于,当我创建一个API下载创建的Excell文件时,它会损坏,并且当我尝试使用MS 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);
            }
        }

最新更新