大数据网格视图导出到excel

时间:2012-03-13 18:36:34

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

我尝试使用C#中的visual studio 2010在winforms应用程序中将datagridview导出为ex​​cel(.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();      
    }

4 个答案:

答案 0 :(得分:2)

在阅读了各种搜索并通过上面的答案后,我发现了这个代码,与我的原始代码相比,它的工作速度非常快(几乎是瞬间完成),只需不到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();

答案 1 :(得分:1)

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

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

Write Array to Excel Range

答案 2 :(得分:0)

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

答案 3 :(得分:0)

尝试ExcelLibrary

  

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

与本机.NET库相比,COM对象通常较慢。

您还应该查看this thread

相关问题