简体   繁体   English

大数据网格视图导出到excel

[英]large datagridview export to excel

im attempting to export the datagridview to excel(.xls) in a winforms application using visual studio 2010 in C#, the problem being it is taking forever to save, so far i have 4220 rows and 20 columns. 我试图使用C#中的visual studio 2010在winforms应用程序中将datagridview导出到excel(.xls),问题是它需要永远保存,到目前为止我有4220行和20列。 Is there a faster way to do this. 有没有更快的方法来做到这一点。 NOTE: I am populating the datagridview from the saved excel file. 注意:我正在从保存的Excel文件中填充datagridview。 I appreciate your help....my save code is as follows: 感谢您的帮助....我的保存代码如下:

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();      
    }

After having read through various searches and through the answers above i came across this code, it works extremely faster (almost instantaneous) as compared with my orginal where it took just under 2 minutes. 在阅读了各种搜索并通过上面的答案后,我发现了这个代码,与我的原始代码相比,它的工作速度非常快(几乎是瞬间),只需不到2分钟。 I am very grateful for your answers above and i will look into these especially the copy and paste method which was an interesting read. 我非常感谢你上面的答案,我会研究这些特别是复制和粘贴方法,这是一个有趣的阅读。 I am a relatively new to this (new hobby) and am only beginning to understand some concepts with regards to exporting datasets etc. I know this is by no means the best way of acheiving what i set out to but it does what i want it to at the moment. 我是一个相对较新的(新的爱好),我只是开始了解有关导出数据集等的一些概念。我知道这绝不是实现我所设定的最佳方式,但它做了我想要的事情到目前为止。 Once again thanks to all who have helped, i am learning alot. 再次感谢所有帮助过的人,我正在学习很多。

            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();

You want to try to minimize the number of calls you make between your .NET code and the Excel process - those are very slow to execute, so populating cell-by-cell takes a long time. 您希望尽量减少在.NET代码和Excel进程之间进行的调用次数 - 执行速度非常慢,因此逐个单元填充需要很长时间。

Better to put the contents of your grid into an array: you can then dump that to the Excel sheet in a single operation. 最好将网格的内容放入数组中:然后可以在单个操作中将其转储到Excel工作表中。

Write Array to Excel Range 将数组写入Excel范围

Using interop to copy cell by cell is very slow. 使用interop逐个单元地复制非常慢。 I would suggest using copy paste instead. 我建议使用复制粘贴代替。 See this msdn article for an example of how to copy the data to the clipboard; 有关如何将数据复制到剪贴板的示例,请参阅此msdn文章 ; you can then use interop to paste the values into the spreadsheet. 然后,您可以使用互操作将值粘贴到电子表格中。

Try ExcelLibrary . 试试ExcelLibrary

"The aim of this project is provide a native .NET solution to create, read and modify Excel files without using COM interop or OLEDB connection." “该项目的目的是提供一个本机.NET解决方案,用于创建,读取和修改Excel文件,而无需使用COM互操作或OLEDB连接。”

COM objects are usually slower compare to native .NET libraries. 与本机.NET库相比,COM对象通常较慢。

You should also take a look at this thread . 你还应该看一下这个帖子

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

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