Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Excel导出占用太多时间_C#_Asp.net_Excel_Interop - Fatal编程技术网

C# Excel导出占用太多时间

C# Excel导出占用太多时间,c#,asp.net,excel,interop,C#,Asp.net,Excel,Interop,在我的asp.net页面(C#)中有7个数据表(行数、列数不同)。我正在使用GridView在页面上显示数据。需要将这些数据表导出到excel文件中,以便我需要第一页上前5个数据表的数据,包括其图形、第2页上第6个数据表的数据和同一excel文件第3页上第7个数据表的数据。 我使用excel interop实现了该解决方案。第一张表(有5个数据表及其图表)正在加载。但是第二张和第三张表单在加载时花费了太多的时间(4000-5000行数据,每张表单中大约有50列) 有没有办法快速加载它们 我正在使

在我的asp.net页面(C#)中有7个数据表(行数、列数不同)。我正在使用GridView在页面上显示数据。需要将这些数据表导出到excel文件中,以便我需要第一页上前5个数据表的数据,包括其图形、第2页上第6个数据表的数据和同一excel文件第3页上第7个数据表的数据。 我使用excel interop实现了该解决方案。第一张表(有5个数据表及其图表)正在加载。但是第二张和第三张表单在加载时花费了太多的时间(4000-5000行数据,每张表单中大约有50列)

有没有办法快速加载它们

我正在使用以下代码:

Worksheet xlWorksheet2 = null;

//Create Excel Sheets

xlSheets = ExcelApp.Sheets;

xlWorksheet2 = (Worksheet)xlSheets.Add(xlSheets[2], Type.Missing, Type.Missing, Type.Missing);

xlWorksheet2.Name = "Dump-1";

table = Dump1();
for (int j = 1; j < table.Columns.Count + 1; j++)
{
    ExcelApp.Cells[1, j] = table.Columns[j - 1].ColumnName;
}

// Storing Each row and column value to excel sheet
for (k = 0; k < table.Rows.Count; k++)
{
    for (int l = 0; l < table.Columns.Count; l++)
    {
        ExcelApp.Cells[k + 2, l + 1] = table.Rows[k].ItemArray[l].ToString();

    }
}
工作表xlWorksheet2=null;
//创建Excel工作表
xlSheets=ExcelApp.Sheets;
xlWorksheet2=(工作表)xlSheets.Add(xlSheets[2],Type.Missing,Type.Missing,Type.Missing);
xlWorksheet2.Name=“Dump-1”;
table=Dump1();
对于(int j=1;j
在VBA中,您可以使用Application.Calculation=xlCalculation手动和Application.screenUpdatement=False来加速这些操作。由于您使用的是excel互操作,因此也可以在C#中执行此操作。我花了一段时间研究此操作。您可以做的是以数组的形式获取表的内容,并将该数组写入excel。比如说,你得到一个由5个元素组成的2D数组,你可以做ExcelApp.Cells(“A1:A5”)。Value=myArray。这可以在vba中使用,我确信可以在c#中使用。我使用了ExcelApp.Cells[“A1:E5”]=myArray,但它给出的错误是“参数不正确。(HRESULT的异常:0x80070057(E_INVALIDARG))”A1:E5?我不知道用它来代替A1:A5会发生什么我解决了这个问题。。。我使用了object[,]arr=newobject[dt.Rows.Count+1,dt.Columns.Count];然后我填充了上面的数组,然后是范围c1=(范围)xlWorksheet2.Cells[1,1];范围c2=(范围)xlWorksheet2.Cells[dt.Rows.Count+1,dt.Columns.Count];范围范围=xl工作表2。获取范围(c1,c2);范围值=arr;