繁体   English   中英

使用格式化从datagridview生成Excel的最快方法

[英]Fastest way to generate Excel from datagridview with formatting

我有一个代码将数据datagridview 导出到Excel工作表,但问题是它很慢,因为它插入数据和格式化每个单元格

如何提高此操作的性能?

以下是我的代码

public static void ExcelExport(DataGridView Dg, string TypePass)
{
    Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
    ExcelApp.Application.Workbooks.Add(Type.Missing);
    Excel_12.ApplicationClass oExcel_12 = null;           //Excel_12 Application
    Excel_12.Workbook oBook = null;                       // Excel_12 Workbook
    Excel_12.Sheets oSheetsColl = null;                   // Excel_12 Worksheets collection
    Excel_12.Worksheet oSheet = null;                     // Excel_12 Worksheet
    Excel_12.Range oRange = null;                         // Cell or Range in worksheet
    Object oMissing = System.Reflection.Missing.Value;
    oExcel_12 = new Excel_12.ApplicationClass();
    oExcel_12.UserControl = true;
    oBook = oExcel_12.Workbooks.Add(oMissing);
    oSheetsColl = oExcel_12.Worksheets;
    oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item("Sheet1");

    oRange = (Excel_12.Range)oSheet.Cells[1, 1];
    oRange.Value2 = "";
    oRange.Font.Name = "Tahoma";
    oRange.Font.Size = 12;
    (oRange).Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
    (oRange).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray);

    if (TypePass.Trim().Length > 0)
    {
        oRange = (Excel_12.Range)oSheet.Cells[2, 1];
        oRange.Value2 = TypePass;
        oRange.Font.Name = "Tahoma";
        oRange.Font.Size = 10;
    }

    int c = 0;

    if (Dg.ColumnHeadersVisible == true)
    {
        for (int j = 0; j < Dg.Columns.Count; j++)
        {
            if (Dg.Columns[j].Visible == true)
            {
                oRange = (Excel_12.Range)oSheet.Cells[4, c + 1];
                oRange.Value2 = Dg.Columns[j].HeaderText + "  ";
                oRange.Font.Bold = true;
                oRange.Font.Name = "Tahoma";
                oRange.Font.Size = 9;
                (oRange).Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
                (oRange).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Teal);
                oExcel_12.Columns.AutoFit();
                c++;
            }
        }
    }

    c = 0;

    for (int i = 0; i < Dg.Rows.Count; i++)
    {

        for (int j = 0; j < Dg.Columns.Count; j++)
        {
            if (Dg.Columns[j].Visible == true)
            {
                oRange = (Excel_12.Range)oSheet.Cells[i + 5, c + 1];
                if (Dg[j, i].Value == null)
                {
                    oRange.Value2 = " ";
                }
                else
                {
                    oRange.Value2 = Dg[j, i].Value.ToString().Replace('\n', ' ') + "  ";
                }

                oRange.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
                oRange.Font.Name = "Tahoma";
                oRange.Font.Size = 8;
                oExcel_12.Columns.AutoFit();
                // oRange.NumberFormat = "dd/MM/yyyy";
                c++;
            }
        }
        c = 0;
    }

    oExcel_12.Visible = true;
    oBook = null;
    oExcel_12 = null;
    GC.Collect();
}

如果您愿意,可以使用Open XML SDK

我已经使用Open XML将导出数据导出到Excel电子表格(.XLSX格式),我可以确保表现很棒。

  • 我可以在2,3秒内生成50,000个单元格电子表格

  • 60秒内的1百万个单元格电子表格[这是10,000行100列电子表格]

你需要知道的:

优势:

  • 您可以创建格式良好的Excel工作表,而无需安装Office软件包。
  • 如果您愿意,也可以将电子表格生成甚至扩展到服务器端。
  • 首先,您会觉得与InterOp相比很难,但是一旦您正确实施,您就可以在任何项目中使用相同的Excel电子表格功能。

如果您决定坚持使用Microsoft.Office.Interop.Excel,则可以通过正确设置范围内的格式和数据来利用代码。

  1. 设置1行的标题样式
  2. 从列*行设置内容样式
  3. 从DataGridView单元格值构建数组然后在范围内写入是一种非常快速的方法: 将数组写入Excel范围

顺便说一句,GC.Collect不能用于关闭COM对象的目的,请参考在C#特别是MS Office应用程序中正确处理COM互操作对象

MS Office Interop很慢,甚至微软也不建议在服务器端使用Interop。 有关更多详细信息,请参阅Microsoft说明为何不使用OLE自动化

Microsoft Excel使用Office 2007发布了XLSX文件格式,并建议使用OpenXML SDK而不是Interop。

如果必须以XLS文件格式保存Excel文件,则可以使用EasyXLS等Excel库。

请参阅以下代码示例,作为将DataGridView导出到Excel的替代方法:

// Create a DataSet and add the DataTable of DataGridView 
DataSet dataSet = new DataSet();
dataSet.Tables.Add((DataTable)dataGridView);//or ((DataTable)dataGridView.DataSource).Copy() to create a copy

// Export Excel file 
ExcelDocument workbook = new ExcelDocument();
workbook.easy_WriteXLSFile_FromDataSet(filePath, dataSet, 
       new EasyXLS.ExcelAutoFormat(EasyXLS.Constants.Styles.AUTOFORMAT_EASYXLS1), 
       "Sheet1");

要导出所需的格式,可以创建自己的ExcelAutoFormat。 检查此代码示例, 了解如何使用格式化将Datagridview导出到C#中的Excel

暂无
暂无

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

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