在 ASP.NET Core 6 中导出到 Excel

问题描述 投票:0回答:1

我正在尝试使用 ASP.NET Core 6 MVC 将数据库值导出为 excel。

这是我的代码

public IActionResult GetExcel()
{
    var excel = (from i in _context.EmployeeBasicInformations 
                 where i.FormCurrentStatus == "CanditateCreated" && 
                       i.IsActive == 1 select i).ToList();

    using (var workbook = new XLWorkbook())
    {
        IXLWorksheet worksheet = workbook.Worksheets.Add("Students");
        worksheet.Cell(1, 1).Value = "Employee Id";
        worksheet.Cell(1, 2).Value = "Employee First Name";
        worksheet.Cell(1, 3).Value = "Employee Last Name";
        worksheet.Cell(1, 4).Value = "Aadhar";

        IXLRange range = worksheet.Range(worksheet.Cell(1, 1).Address, worksheet.Cell(1, 4).Address);
        range.Style.Fill.SetBackgroundColor(XLColor.Almond);

        int index = 1;

        foreach (var item in excel)
        {
            index++;

            worksheet.Cell(index, 1).Value = item.EmployeeId;
            worksheet.Cell(index, 2).Value = item.EmployeeFirstName;
            worksheet.Cell(index, 3).Value = item.EmployeeLastName;
            worksheet.Cell(index, 4).Value = item.EmployeeAadharCardNumber;
         }

         using (var stream = new MemoryStream())
         {
             workbook.SaveAs(stream);
             var content = stream.ToArray();
             string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

             var strDate = DateTime.Now.ToString("yyyyMMdd");
             string filename = string.Format($"Students_{strDate}.xlsx");

             return File(content, contentType, filename);
         }
     }
}

在检查时,我能够看到生成的文件,但我收到以下错误:

“stream.ReadTimeout”引发了“System.InvalidOperationException”类型的异常
“stream.WriteTimeout”引发了“System.InvalidOperationException”类型的异常

我不确定发生了什么——请帮忙。

asp.net-core-mvc .net-6.0 export-to-excel
1个回答
0
投票

问题似乎出在 MemoryStream 上。在读取之前尝试将其位置设置为 0。这是您的代码的更新版本:

public IActionResult GetExcel()
{
var excel = (from i in 
_context.EmployeeBasicInformations 
where i.FormCurrentStatus == 
"CanditateCreated" && i.IsActive == 1 
select i).ToList();

using (var workbook = new XLWorkbook())
{
    IXLWorksheet worksheet =
        
workbook.Worksheets.Add("Students");
    worksheet.Cell(1, 1).Value = 
"Employee Id";
    worksheet.Cell(1, 2).Value = 
"Employee First Name";
    worksheet.Cell(1, 3).Value = 
"Employee Last Name";
    worksheet.Cell(1, 4).Value = 
"Aadhar";

    IXLRange range = 
worksheet.Range(worksheet.Cell(1, 
1).Address, worksheet.Cell(1, 
4).Address);  
range.Style.Fill.SetBackgroundColor(
XLColor.Almond);

    int index = 1;

    foreach (var item in excel)
    {
        index++;

        worksheet.Cell(index, 1).Value 
= item.EmployeeId;
        worksheet.Cell(index, 2).Value 
= item.EmployeeFirstName;
        worksheet.Cell(index, 3).Value 
= item.EmployeeLastName;
        worksheet.Cell(index, 4).Value 
= item.EmployeeAadharCardNumber;

    }

    using (var stream = new 
MemoryStream())
    {
        workbook.SaveAs(stream);
        stream.Position = 0; // Set the 
position to 0
        var content = stream.ToArray();
        string contentType = 
"application/vnd.openxmlformats- 
officedocument.spreadsheetml.sheet";

        var strDate = 
DateTime.Now.ToString("yyyyMMdd");
        string filename =  string.Format($"Students_{strDate}.xlsx");

        return File(content, 
contentType, filename);
    }
}
}

通过将 MemoryStream 的位置设置为 0,您可以确保流在转换为数组时从头开始读取。

© www.soinside.com 2019 - 2024. All rights reserved.