簡體   English   中英

如何在C#中加快Excel互操作過程

[英]how to speed up excel interop process in c#

這是我的代碼。 請幫我的朋友們。 此代碼可以將任何文本文檔轉換為excel。 但是,在大型文檔中,需要花費大量時間。 我該如何解決?

Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    xlApp.ScreenUpdating=false;
    object misValue = System.Reflection.Missing.Value;
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        FileInfo theSourceFile = new FileInfo(@"" + file); 
        StreamReader reader = theSourceFile.OpenText();
        int raw = 1; int column = 1; 
        String text = "";
          do
             {
               text = reader.ReadLine();
               if (text != null)
                  {
                    string[] ss = text.Split('|');
                    int index = 0; double result;
                    //WRITING DATA LINES
                    for (int i = 1; i < ss.Length; i++)
                      {
                       if (!ss[index].Contains('.')) //recognising strings by filtering currency values using "." sign (decimal point)
                          {
                           xlWorkSheet.Cells[raw, column] = ss[index];
                           index++; column++; 
                           }

                        else if (double.TryParse(ss[index], out result))//writing and formating currency values
                           {   xlWorkSheet.Cells[raw, column] = String.Format("{0:n}", result);
                                index++; column++; 
                            }
                         else
                            {
                              xlWorkSheet.Cells[raw, column] = ss[index];//writing "." containing non currcy values
                              index++; column++;
                            }
                      }
                  }
                    raw++; column = 1;
          } while (text != null);
            xlWorkSheet.Columns.AutoFit();
           xlWorkBook.SaveAs(@"" + textBox6.Text + @"\" + line_dup + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
     try
        {
          Marshal.ReleaseComObject(xlWorkSheet);
          Marshal.ReleaseComObject(xlWorkBook);
          Marshal.ReleaseComObject(xlApp);
          xlWorkBook.Close(true, misValue, misValue);
          xlApp.Quit();
        }
    catch (Exception) { }
          foreach (Process clsProcess in Process.GetProcesses())
         if (clsProcess.ProcessName.Equals("EXCEL"))  //KILLING EXCELL Process
             clsProcess.Kill();

          richTextBox1.Text += "\n" + line + "\t" + "excell file created";

        MessageBox.Show("Excel files created , you can find the files in @" + textBox6.Text + line_dup + ".xls");
     foreach (Process clsProcess in Process.GetProcesses())
       if (clsProcess.ProcessName.Equals("EXCEL"))  //KILLING EXCELL Process
         clsProcess.Kill();

將數據放入每個單元格確實很慢。 您應該嘗試對行,列甚至整個區域使用批量插入方法。 我插入了大約500%的內容。 20k行和10列。 唯一的陷阱是您必須在內存中創建對象,而不能進行流式處理。

使用對象數組,這是一些工作控制台應用程序。

打印100,000行可能需要2秒鍾左右。

        //here's some class I used
        public class Movie
        {
            public int MovieID { get; set; }
            public string MovieName { get; set; }

            public System.Data.DataTable MovieListDataTable()
            {
                System.Data.DataTable dataTable = new System.Data.DataTable();
                dataTable.Columns.Add("MovieID", typeof(int));
                dataTable.Columns.Add("MovieName", typeof(string));

                for (int x = 0; x <= 100000; x++)
                {
                    dataTable.Rows.Add(new object[] { x, "Star Wars " + x.ToString() });
                }

                return dataTable;
            }

            public String GetExcelColumnName(int columnIndex)
            {
                if (columnIndex < 0)
                {
                    throw new ArgumentOutOfRangeException("columnIndex: " + columnIndex);
                }
                Stack<char> stack = new Stack<char>();
                while (columnIndex >= 0)
                {
                    stack.Push((char)('A' + (columnIndex % 26)));
                    columnIndex = (columnIndex / 26) - 1;
                }
                return new String(stack.ToArray());
            }
        }

        // then inside the MAIN
        static void Main(string[] args)
        {
            Application myExcelApp = new Application();
            _Worksheet myExcelWorkSheet; //= new _Worksheet();

            Movie movie = new Movie();

            //ensure that you have a Book1.xlsx the same path as this program.
            string ExcelFilePath = AppDomain.CurrentDomain.BaseDirectory + "Book1.xlsx";
            string letterStart = "";
            string letterEnd = "";

            int row = 0, col = 0;

            var toPrint = movie.MovieListDataTable();
            row = toPrint.Rows.Count;
            col = toPrint.Columns.Count;

            letterStart = movie.GetExcelColumnName(0);
            letterEnd = movie.GetExcelColumnName(col - 1);

            Workbook myExcelWorkBook = myExcelApp.Workbooks.Open(ExcelFilePath);
            myExcelWorkSheet = myExcelWorkBook.Worksheets[1];

            var dataBulk = new object[row, movie.GetType().GetProperties().Count()];

            for (int printx = 0; printx < row; printx++)
            {
                for (int printy = 0; printy < col; printy++)
                {
                    dataBulk[printx, printy] = toPrint.Rows[printx][printy].ToString();
                }
            }

            //print start cell range
            Range startCellRange = myExcelWorkSheet.Range[letterStart + 1.ToString(), letterStart + 1.ToString()];
            //print end cell range
            Range endCellRange = myExcelWorkSheet.Range[letterEnd + row.ToString(), letterEnd + row.ToString().ToString()];
            //full write cell range
            Range writeRange = myExcelWorkSheet.Range[startCellRange, endCellRange];

            //data to print within range
            writeRange.Value2 = dataBulk;

            myExcelWorkBook.Save();
            myExcelWorkBook.Close();

            Marshal.ReleaseComObject(myExcelWorkBook);
            Marshal.ReleaseComObject(myExcelWorkSheet);
            Marshal.ReleaseComObject(myExcelApp);
        }

暫無
暫無

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

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