How to speed up dumping a DataTable into an Excel worksheet?

后端 未结 7 1404
礼貌的吻别
礼貌的吻别 2020-12-15 07:37

I have the following routine that dumps a DataTable into an Excel worksheet.

    private void RenderDataTableOnXlSheet(DataTable dt, Excel.Worksheet xlWk,          


        
相关标签:
7条回答
  • 2020-12-15 08:00

    Do you have a specific requirement to go the COM automation route? If not, you have a few other options.

    1. Use the OLEDB provider to create/write to an Excel file
      http://support.microsoft.com/kb/316934

    2. Use a third party library to write to Excel. Depending on your licensing requirements there are a few options. Update: A good free library is NPOI http://npoi.codeplex.com/

    3. Write the data to a csv file, and load that into Excel

    4. Write the data as XML which can be loaded into Excel.

    5. Use the Open XML SDK
      http://www.microsoft.com/downloads/details.aspx?familyid=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en

    0 讨论(0)
  • 2020-12-15 08:01

    If you have a recordset, the fastest way to write to Excel is CopyFromRecordset.

    0 讨论(0)
  • 2020-12-15 08:02

    You could create an Excel add-in, with VBA code to do all your db heavy lifting. from .NET, all you'd need to do is instantiate Excel, add the add-in, and call the Excel VBA routine, passing any parameters to it that it needs to execute your SQL statements.

    0 讨论(0)
  • 2020-12-15 08:06

    I agree with Charles. Interop is really slow. But try this:

    private void RenderDataTableOnXlSheet(DataTable dt, Excel.Worksheet xlWk, 
                                        string [] columnNames, string [] fieldNames)
    {
        // render the column names (e.g. headers)
        int columnLength = columnNames.Length;
        for (int i = 0; i < columnLength; i++)
            xlWk.Cells[1, i + 1] = columnNames[i];
    
        // render the data 
            int fieldLength = fieldNames.Length;
            int rowCount = dt.Rows.Count;
            for (int j = 0; j < rowCount; j++)
            { 
                for (int i = 0; i < fieldLength; i++)
                {
                    xlWk.Cells[j + 2, i + 1] = dt.Rows[j][fieldNames[i]].ToString();
                }
            }
    }
    

    HTH

    0 讨论(0)
  • 2020-12-15 08:12

    Instead of setting cell values one by one, do it in a batch.

    Step 1. Transfer the data from your DataTable into an array with the same dimensions.

    Step 2. Define an Excel Range object that spans the appropriate range.

    Step 3. Set the Range.Value to the array.

    This will be a lot faster because you will have a total two calls across the Interop boundary (one to get the Range object, one to set its value), instead of two per cell (get cell, set value).

    There is some sample code at MSDN KB article 302096.

    0 讨论(0)
  • 2020-12-15 08:19

    Interop is inherently very slow. There is a large overhead associated with each call. To speed it up try writing back an object array of data to a range of cells in one assignment statement.

    Or if this is a serious problem try using one of the Managed Code Excel extensions that can read/write data using managed code via the XLL interface. (Addin Express, Managed XLL etc.)

    0 讨论(0)
提交回复
热议问题