优化c#excel读/写文件

时间:2015-03-17 19:38:43

标签: c# .net excel optimization excel-interop

所以我有大约2200行这个excel,我需要读取和写入txt文件,问题是它花了太多时间,我被告知读取/写入文件通常需要时间,因为它的性质,所以我尝试只读取一次excel文件,使用stringBuilder并写入每行(避免尝试存储所有的文本和写入整个.txt文件)

但是,有什么办法可以加快速度吗?

选择较小的范围,只有1行?使用\ n作为换行符填写一个巨大的字符串,然后将所有内容写入.txt?

这是我的代码示例

using Excel = Microsoft.Office.Interop.Excel;
[...]
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open("C:/Users/MyUser/Desktop/SomeFolder/my_excel.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range allRange = xlWorkSheet.UsedRange;
try
{
    System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\test.txt");
    String line = "";
    //StringBuilder line;
    for (int row = 1; row <= allRange.Rows.Count; row++) //These are up to thousand sometimes
    {
        if (allRange.Value2[row, 1] != "")
        {
            //line = new StringBuilder();
            for (int column = 1; column <= 6; column++)
            {
                //Console.WriteLine(allRange.Value2[row, column]);
                line += allRange.Value2[row, column];
                if (column != 6)
                {
                    line += "|";
                    //line.Append("|");
                }
            }
            file.WriteLine(line);
            line = "";
        }
        else
        {
            MessageBox.Show("Should've not reached here.");
            break;
        }
    }
    file.Close();
    }
catch (Exception ex)
{
    MessageBox.Show("Couldn't write file: " + ex.ToString());
}

顺便说一句,我使用的是.NET v4.0.30319 ......我想(在Environment.Version.ToString()上说)

或.NET v4.5.51209(&#34;帮助&#34;&gt;&#34;关于Microsoft Visual Studio&#34;

2 个答案:

答案 0 :(得分:1)

我认为这段代码速度慢的主要原因是Excel Interop的使用受到了影响。这很慢。而不是尝试使用OpenXML SDK - 它的库来操作Office 2007+文档(包括* .xlsx)。它比ExcelInterop快得多,并且它不需要在机器上安装Excel实例。主要缺点是它无法打开XLS文件。以下是如何阅读大型文档的示例:https://msdn.microsoft.com/EN-US/library/office/gg575571.aspx

还尝试使用StopWatch或任何分析器,并测量代码中最慢的部分。

答案 1 :(得分:0)

我仍然是Excel Interop的新手,但这里有一些我最近改进过的代码。表演从30秒到2秒不等。

                        //This method is very slow.
                        // Storing Each row and column value to excel sheet
                        //for (int k = 0, k2 = 2; k < table.Rows.Count; k++, k2++)
                        //{
                        //    for (int l = 0, l1 = 1; l < table.Columns.Count; l++, l1++)
                        //    {
                        //        //ExcelApp.Cells[k2, l1] =
                        //        //    table.Rows[k].ItemArray[l].ToString();
                        //        ExcelApp.Cells[k2, l1] =
                        //            table.Rows[k][l].ToString();
                        //    }
                        //}

                        ////////////////

                        //See if this method is faster
                        // transform formated data into string[,]
//                        var excelData = new string[table.Rows.Count, table.Columns.Count];
                        var excelData = new object[table.Rows.Count, table.Columns.Count];
                        for (int rowJ = 0; rowJ < table.Rows.Count; rowJ++)
                        {
                            for (int colI = 0; colI < table.Columns.Count; colI++)
                            {
//                                excelData[rowJ, colI] = table.Rows[rowJ][colI].ToString();
                                excelData[rowJ, colI] = table.Rows[rowJ][colI];
                                //excelData[colI, rowJ] = "test";
                            }
                        }
                        //<Code to set startLoc and endLoc removed>

                        Range valRange = ExcelApp.get_Range(startLoc, endLoc);
                        valRange.Value2 = excelData;