【问题标题】:large datagridview export to excel大datagridview导出到excel
【发布时间】:2012-03-30 04:57:39
【问题描述】:

我试图在 Winforms 应用程序中使用 Visual Studio 2010 在 C# 中将 datagridview 导出到 excel(.xls),问题是它需要永远保存,到目前为止我有 4220 行和 20 列。有没有更快的方法来做到这一点。注意:我正在从保存的 excel 文件中填充 datagridview。感谢您的帮助....我的保存代码如下:

private void btnSave_Click(object sender, EventArgs e)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        // Get the Header from dataGridView
        for (int h = 1; h < dataGridView1.Columns.Count + 1; h++)
        {
            xlWorkSheet.Cells[1, h] = dataGridView1.Columns[h - 1].HeaderText;
        }

        // Get the Cell Values
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {
                DataGridViewCell cell = dataGridView1[j, i];
                xlWorkSheet.Cells[i + 2, j + 1] = cell.Value;
            }
        }

        //xlWorkBook.SaveCopyAs("\\FORM TEST.xlsx");
        xlWorkBook.SaveAs("\\GB STOCK.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();

        xlApp = null;
        xlWorkBook = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();      
    }

【问题讨论】:

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


    【解决方案1】:

    在阅读了各种搜索并通过上面的答案后,我发现了这段代码,与我的原始代码相比,它的运行速度非常快(几乎是瞬时的),它只需要不到 2 分钟。我非常感谢您在上面的回答,我将研究这些,尤其是复制和粘贴方法,这是一个有趣的阅读。我对此(新爱好)相对较新,并且才刚刚开始了解有关导出数据集等的一些概念。 我知道这绝不是实现我的目标的最佳方式,但它可以满足我目前的需求。再次感谢所有帮助过的人,我学到了很多。

                int cols;
                //open file 
                StreamWriter wr = new StreamWriter("GB STOCK.csv", false, Encoding.UTF8);
    
                //determine the number of columns and write columns to file 
                cols = dgvStock.Columns.Count;
                for (int i = 0; i < cols; i++)
                    { 
                        wr.Write(dgvStock.Columns[i].Name.ToString().ToUpper() + ",");
                    } 
                wr.WriteLine();
    
                //write rows to excel file
                for (int i = 0; i < (dgvStock.Rows.Count); i++)
                    { 
                        for (int j = 0; j < cols; j++)
                            { 
                                if (dgvStock.Rows[i].Cells[j].Value != null)
                                    {
                                        wr.Write(dgvStock.Rows[i].Cells[j].Value + ",");
                                    }
                                else 
                                    {
                                        wr.Write(",");
                                    }
                            }
    
                        wr.WriteLine();
                    }
                    //close file
                    wr.Close();
    

    【讨论】:

      【解决方案2】:

      您希望尽量减少您在 .NET 代码和 Excel 进程之间进行的调用次数 - 这些调用执行起来非常缓慢,因此逐个单元格填充需要很长时间。

      最好将网格的内容放入数组中:然后您可以通过一次操作将其转储到 Excel 工作表中。

      Write Array to Excel Range

      【讨论】:

        【解决方案3】:

        使用互操作逐单元复制非常慢。我建议改用复制粘贴。有关如何将数据复制到剪贴板的示例,请参见this msdn article;然后,您可以使用互操作将值粘贴到电子表格中。

        【讨论】:

          【解决方案4】:

          试试ExcelLibrary

          “这个项目的目的是提供一个原生 .NET 解决方案来创建、读取和修改 Excel 文件,而无需使用 COM 互操作或 OLEDB 连接。”

          COM 对象通常比原生 .NET 库慢。

          你也应该看看this thread

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-04-16
            • 1970-01-01
            • 1970-01-01
            • 2014-04-09
            • 2013-09-11
            • 2011-08-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多