0

Here is my code. Please help me friends. This code can convert any text document to excel. But in large documents it takes so much time. How do i solve this?

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();
Yogi
  • 9,174
  • 2
  • 46
  • 61
dilhan
  • 19
  • 1
  • 7
  • 1
    This belongs to [Code Review](http://codereview.stackexchange.com/) – bansi Dec 22 '15 at 05:54
  • 1
    @bansi, Please read a [Guide to Code Review for Stack Overflow users](http://meta.codereview.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users). Especially if code is broken, it is not suited for Code Review. And this code looks broken as the braces don't seem to match up or there is some really strange indentation issues – holroy Dec 22 '15 at 05:58
  • 1
    @holroy as per OP the code is working and need optimization. Perfect candidate for Code Review. – bansi Dec 22 '15 at 06:00
  • A quick paste into VS reveals no missing braces. As long as the code functions (which the OP claims it does), it should be on topic for Code Review – Quill Dec 22 '15 at 06:02
  • When looking at code I see 8 opening braces, and 12 closing braces. Either I'm blind, or this code is broken. – holroy Dec 22 '15 at 06:02
  • i removed the heading writing part from the code while the question posting. please ignore the brace missings. i think the slowing problem is in the for loop part. could you can give me a solution? – dilhan Dec 22 '15 at 06:08
  • If you post it to [Code Review](http://codereview.stackexchange.com) make sure to include the heading, so that there are no brace problems. And as long as it does work, we can help you out. – Quill Dec 22 '15 at 06:15
  • @dilhan, Sorry, for branching off into another discussion, but as it stands the code is broken, and it'll be closed on Code Review. If you post the actual working code, we could help you, but we do need to see the entire code. If not, you are better off here at Stack Overflow. – holroy Dec 22 '15 at 06:17

2 Answers2

0

Putting data in each cell is really slow. You should try to use the bulk insert methods for rows, columns or even entire areas. I got an increase of around 500% for inserting approx. 20k rows and ten columns. The only pitfall is that you have to create your objects in memory, no streaming.

tziemek
  • 61
  • 1
  • 5
  • yes.i'm writing data cell by cell because i have to check the each of the value whether it is string or decimal. – dilhan Dec 23 '15 at 05:33
  • is there were the any option to allocate ram to the program? – dilhan Dec 23 '15 at 05:35
  • You can still check each cell, just use get_Range to write an array. See [WriteArray](http://stackoverflow.com/questions/536636/write-array-to-excel-range) and [SpeedUpDumping](http://stackoverflow.com/questions/2692979/how-to-speed-up-dumping-a-datatable-into-an-excel-worksheet). – tziemek Jan 12 '16 at 11:09
0

use object array, here's some working console app.

printing the 100,000 rows should probably take you around 2 secs.

        //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);
        }
ken lacoste
  • 894
  • 8
  • 22