首页 > 解决方案 > 从数据表更新excel表列时需要避免数据表中5k条记录的延迟

问题描述

//从数据表中检索数据时需要更新Excel工作表列在此处输入代码

     foreach (DataRow datarow in dt.Rows)
        {
            int[] colNumber = new int[] { 9,5,13,24,111,17,76,34,38 }; 
            rowcount += 1;
           for (int i = 0; i < colNumber.Length; i++)
            { string value = datarow[i].ToString();
           ws.Cells[rowcount, colNumber[i]] = value;
            }
        }

标签: c#asp.net.netentity-frameworklinq

解决方案


使用 EntityFramework 将无法解决该问题,因为恐怕您关注的是错误的区域。这种延迟的主要原因是您试图访问dt.Rows.

为此,我建议使用 GemBox.Spreadsheet。您可以直接将数据表附加到工作表。此外,您不需要将值显式转换为字符串。

----------
// Code sample.
private void Download()
{
    DataTable datatable=yourDatatable;// callStore Procedure to fetch  DataTable
    ExcelFile csvFile = new ExcelFile();//GemBox.Spreadsheet ExcelFile  
    ExcelWorksheet ws = csvFile.Worksheets.Add("YourWorkSheetName");

    if (ws != null)
    {
        ws.InsertDataTable(datatable, 0, 0, true);
        // Use MemoryStream to save or to send it to client as response.
    }
}

----------

推荐阅读