使用c#读取Excel文件并写入文本文件非常慢

时间:2016-07-08 19:40:57

标签: c# wpf excel

我在c#wpf应用程序中有以下代码。我正在读取Excel文件,删除隐藏的字符并尝试保留单元格格式,然后将数据写入管道分隔的文本文件。这段代码看起来很直接,但速度很慢。关于为什么以及如何改进流程的任何想法?

    private void ReadWriteExcelData(string strFileName)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        Excel.Range range, colrange, rowrange;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(strFileName, 0, true, 5, "", "", true,
            Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

        Excel.Sheets excelSheets = xlWorkBook.Worksheets;
        if (blnLetExcelDecide)
            {
                range = xlWorkSheet.UsedRange;
            }
            else
            {
                Excel.Range c1 = xlWorkSheet.Cells[lngExcelStartRow, strExcelStartCol];
                Excel.Range c2 = xlWorkSheet.Cells[lngExcelEndRow, strExcelEndCol];
                range = (Excel.Range)xlWorkSheet.get_Range(c1, c2);
            }

            colrange = range.Columns;
            lngNumCols = colrange.Count;
            rowrange = range.Rows;
            lngNumRows = rowrange.Count;

            object[,] values = (object[,])range.Value;
            string[] Fields = new string[lngNumCols];
            int NumRow = 1;

            while (NumRow <= values.GetLength(0))
            {
                strDataRow = "";

                for (lngColCnt = 1; lngColCnt <= lngNumCols; lngColCnt++)
                {
                    strCellData = range[NumRow, lngColCnt].Text;
                    strCellData = strCellData.TrimStart(' ');


                    if (strCellData == null)
                    {
                        strCellData = string.Empty;
                    }
                    else
                    {
                        strCellData = strCellData.Replace("\r\n", " ").Replace("\n", " ").Replace("\r", " ");
                    }

                    if (lngColCnt == lngNumCols)
                    {
                        strDataRow += strCellData;
                    }
                    else
                    {
                        strDataRow += strCellData + "|";
                    }
                }

                WriteDataRow(strDataRow, strFullOutputFileName);

                if (NumRow % intModNumber == 0)
                {
                    dblProgressPct = ((double)NumRow / (double)lngNumRows);
                    dblProgress = Math.Round((dblProgressPct * 100), 0);
                    prgIndicator.Width = dblProgress * 4;
                    lblPrctPrgrs.Content = dblProgress + "%";

                    grdProgressIndicator.InvalidateVisual();
                    System.Windows.Forms.Application.DoEvents();
                }

                NumRow++;
            }
       }  

这是WriteDataRow例程:

    public void WriteDataRow(string strDataRow, string strFullFileName)
    {
        using (StreamWriter file = new StreamWriter(@strFullFileName, true, Encoding.GetEncoding("iso-8859-1")))
        {
            file.WriteLine(strDataRow);
        }
    }

2 个答案:

答案 0 :(得分:0)

这是一种涉及使用一些VBA来读取所有单元格的方法。文字值。

首先,在常规模块中创建一个包含此函数的xlsm文件:

Public Function GetText(strWB As String, strSheet As String, _
                        strAddress As String) As Variant()
    Dim rng As Range, arr() As Variant, r As Long, c As Long
    Set rng = Workbooks(strWB).Worksheets(strSheet).Range(strAddress)
    rng.Columns.AutoFit 'avoid getting "######" !
    ReDim arr(0 To rng.Rows.Count - 1, 0 To rng.Columns.Count - 1)
    For r = 1 To rng.Rows.Count
    For c = 1 To rng.Columns.Count
        arr(r - 1, c - 1) = rng.Cells(r, c).Text
    Next c
    Next r
    GetText = arr
End Function

打开数据文件后,使用宏打开文件:

Excel.Workbook xlCodeWb = xlApp.Workbooks.Open(@"D:\Folder\Stuff\TheMacro.xlsm"); 

然后调用宏:

object[,] values = xlApp.Run("'" + xlCodeWb.Name + "'!GetText", 
                   xlWorkBook.Name, xlWorkSheet.Name, range.Address); 

values现在是工作表中所有Text值的2D数组,没有必须在跨越流程边界的单独调用中挑选每个值的开销。你可以迭代数组并编写&#34;清理过的&#34;值到您的文件。

顺便说一句,您应该考虑在main方法中打开并写入输出文件:打开一次,然后写入行,只有在您完成后关闭它。无需为每一行重新打开它。

答案 1 :(得分:0)

我添加了格式类型为“#,## 0.00_”Red的检查。仅当此单元格具有此格式时,我才会执行Convert.ToString(范围[NumRow,lngColCnt] .Value2)。