【问题标题】:Need to Avoid delays for 5k records in data table while updating excel sheet column from data table从数据表更新excel表列时需要避免数据表中5k条记录的延迟
【发布时间】:2019-06-18 23:27:34
【问题描述】:

//从数据表中检索数据时需要更新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;
            }
        }

【问题讨论】:

  • LINQ 不一定能节省时间。
  • 我宁愿输出Console.WriteLine(rowcount); 而不是那么频繁,例如让我们报告每个100th 记录:if (rowcount % 100 == 0) Console.WriteLine(rowcount);
  • 实际上它更新了 10 万条记录,我需要 4 小时才能更新。 Linq 可以节省时间
  • @Sweeper- 在这种情况下,如果我使用 linq,它实际上会帮助我更新 excel 工作表数据

标签: c# asp.net .net entity-framework linq


【解决方案1】:

使用 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.
    }
}

----------

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 2019-10-23
    相关资源
    最近更新 更多