简体   繁体   English

C#Excel Interop性能加载数据

[英]c# excel interop performance loading data

I have an excel interop project that is being runned from a ribbon. 我有一个从功能区运行的excel互操作项目。 My issue is that when loading data of 64k with 30 columns it is insanely slow. 我的问题是,当加载具有30列的64k数据时,速度非常慢。 So I created another set of projects, one project that creates and launches an excel sheet from winforms window via button click and loads 100k rows just fine, the other project is an excel add in project created from visual studio. 因此,我创建了另一组项目,一个项目通过单击按钮从winforms窗口创建并启动excel工作表,并且可以正常加载10万行,另一个项目是从Visual Studio创建的excel附加项目。 Loading data in the add in is incredibly slow. 在加载项中加载数据的速度非常慢。 from examing it while loading rows which is done by creating a range on the sheet and setting 2 dimensional array to that range takes minutes bec there seems to be threads exiting during the process. 通过在工作表上创建范围并将二维数组设置为该范围来完成加载行的检查需要花费几分钟,因为在此过程中似乎有线程退出。 Can anyone help me get clarity or a way to speed up the process. 任何人都可以帮助我弄清楚情况或加快流程的方法。 Thanks Code to where I am setting value to the sheet: 感谢代码到我在表上设置值的位置:

public static Interop.Range AddData(DataTable dataTable, Interop.Worksheet sheetToAddTo, int rowOffset) {
        Interop.Range tableRange = sheetToAddTo.Range[sheetToAddTo.Cells[1 + rowOffset, 1], sheetToAddTo.Cells[1 + rowOffset, 1]];
        if (dataTable != null && dataTable.Columns.Count > 0)
        {
            //create the object to store the column names
            object[,] columnNames;
            columnNames = new object[1, dataTable.Columns.Count];

            //add the columns names from the datatable
            for (int i = 0; i < dataTable.Columns.Count; i++)
                columnNames[0, i] = dataTable.Columns[i].ColumnName;

            //get a range object that the columns will be added to
            Interop.Range columnsNamesRange = (Interop.Range)sheetToAddTo.Range[sheetToAddTo.Cells[1 + rowOffset, 1], sheetToAddTo.Cells[1 + rowOffset, dataTable.Columns.Count]];

            //a simple assignement allows the data to be transferred quickly
            columnsNamesRange.Value2 = columnNames;

            //release the columsn range object now it is finished with
            columnsNamesRange = null;

            if (dataTable.Rows.Count > 0)
            {
                //create the object to store the dataTable data
                object[,] rowData;
                rowData = new object[dataTable.Rows.Count, dataTable.Columns.Count];

                //insert the data into the object[,]
                for (int iRow = 0; iRow < dataTable.Rows.Count; iRow++)
                    for (int iCol = 0; iCol < dataTable.Columns.Count; iCol++)
                        rowData[iRow, iCol] = dataTable.Rows[iRow][iCol];

                //get a range to add the table data into 
                //it is one row down to avoid the previously added columns
                Interop.Range dataCells = (Interop.Range)sheetToAddTo.Range[sheetToAddTo.Cells[2 + rowOffset, 1], sheetToAddTo.Cells[dataTable.Rows.Count + rowOffset + 1, dataTable.Columns.Count]];

                //assign data to worksheet
                dataCells.Value2 = rowData;

                //release range
                dataCells = null;
            }

            //return the range to the new data
            if (dataTable.Rows.Count > 0)
                tableRange = sheetToAddTo.Range[sheetToAddTo.Cells[1 + rowOffset, 1], sheetToAddTo.Cells[dataTable.Rows.Count + rowOffset + 1, dataTable.Columns.Count]];
            else
                tableRange = sheetToAddTo.Range[sheetToAddTo.Cells[1 + rowOffset, 1], sheetToAddTo.Cells[1 + rowOffset + 1, dataTable.Columns.Count]];
        }
        return tableRange;
    }

Most of the times, this is an issue with interop calls. 在大多数情况下,这是互操作调用的问题。

You shouldn't do this: 您不应该这样做:

for (int col = 0; col < 30 ; col++)
{
    for (int row = 0; row < 30 ; row++)
    {
        object theValue = range.Cells[row,col].Value;
    }
}

but: 但:

object[,] allValues = range.Value;

This increases performance a lot. 这大大提高了性能。

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

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