簡體   English   中英

優化C#Excel讀取/寫入文件

[英]Optimizing c# excel read / write to file

因此,我有大約2200行需要讀取和寫入txt文件的excel,問題是它花費了太多時間,有人告訴我讀寫文件通常需要時間,因為它是自然的 ,所以我嘗試只讀取一次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(對“幫助”>“關於Microsoft Visual Studio”說)

我認為這段代碼很慢的主要原因是因為使用Excel Interop。 非常慢 而不是嘗試使用OpenXML SDK-它是用來處理Office 2007+文檔(包括* .xlsx)的庫。 ExcelInterop的速度要快得多,並且不需要在計算機上安裝Excel實例。 主要缺點是它無法打開XLS文件。 這是如何閱讀大型文檔的示例: https : //msdn.microsoft.com/EN-US/library/office/gg575571.aspx

另外,請嘗試使用StopWatch或任何探查器,並測量代碼中最慢的部分。

我對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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM