【问题标题】:Fastest way to generate Excel from datagridview with formatting从带有格式的 datagridview 生成 Excel 的最快方法
【发布时间】:2015-02-16 19:49:03
【问题描述】:

我有一个代码可以将数据从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();
}

【问题讨论】:

  • 您可以使用OleDb,但不会执行格式化操作,OleDb 会将您的 Excel 视为数据库。

标签: c# excel winforms datagridview export-to-excel


【解决方案1】:

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

我使用 Open XML 将数据导出到 Excel 电子表格(.XLSX 格式),我可以保证性能非常好。

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

  • 60 秒内 100 万个单元格电子表格 [即 10,000 行 100列电子表格]

你需要知道的:

  • 了解电子表格的结构
  • 遵循给定的指南 herehere
  • 了解造型 [一种可以实现多种可能性的 PRO 级别]
  • Open XML Productivity tool 合作;将缓解您的学习曲线guide

优势:

  • 无需 Office 即可创建格式良好的 Excel 工作表 安装包。
  • 您还可以将电子表格生成扩展到服务器端,如果 你喜欢。
  • 一开始你会觉得它比 InterOp 难,但是一旦你正确地实现了,你就可以在ANY项目中使用同样的 Excel 电子表格功能。!

【讨论】:

  • 关于 OpenXml,我支持他。自己用!
  • 从 2007 年开始的开放 XML 文件格式。所以是的,对于 2007 年的 upwords。对于 2003 和 XP 版本,您有服务包,可让您使用 .XLSX 文件链接 - support.microsoft.com/kb/924074
  • 抱歉回复晚了,但问题仍未解决
【解决方案2】:

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

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

顺便说一句,GC.Collect 不能用于关闭 COM 对象,请参考Proper disposal of COM interop objects in C# particularly MS Office applications

【讨论】:

  • "GC.Collect" 这里仅用于删除它用于创建 excel "Book" 的内存,我有点喜欢你的想法,所以我赞成你的答案,但我需要一种最少的方法生成 tge 报告的时间。
【解决方案3】:

MS Office Interop 很慢,甚至微软也不推荐在服务器端使用 Interop。有关更多详细信息,请参阅 Microsoft 在 why not to use OLE Automation 上所说的内容。

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

如果您必须将 Excel 文件保存为 XLS 文件格式,您可以使用诸如 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。在how to export datagridview to Excel in C# with formatting 上查看此代码示例。

【讨论】:

  • 是的,确实如此。 xls 文件格式适用于 Office 2003。代码示例适用于 Office 2003。
猜你喜欢
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-26
  • 2011-06-11
相关资源
最近更新 更多