繁体   English   中英

下载Excel文件时损坏

[英]The Excel File gets corrupted when it gets downloaded

我正在使用Syncfusion库在Asp.Net Core2.2中创建excell文件。 问题是,当我创建一个Api以下载创建的Excell文件时,该文件已损坏,当我尝试使用MS Excell打开它时,它说文件格式和文件扩展名不匹配。 这是我创建Excell和My 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);
            }

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM