简体   繁体   中英

Fast split excel file by value in c#

I need to split excel file by selected row. I have quite big amount of row so split by hand took too much time.

 worksheet.Range["A1"].EntireRow.Copy();
                Microsoft.Office.Interop.Excel.Workbook nowy = excel.Workbooks.Add();
                Excel.Worksheet sh = nowy.Sheets.Add();
                sh.Name = "test";

                Excel.Range r = sh.get_Range("A1", "A1").EntireRow;
                r.Insert(Excel.XlInsertShiftDirection.xlShiftDown);
                int y;
                int limit=range.Rows.Count;
                for ( y= 2; y <= limit; y++)
                {
                   ile++;
                   worksheet.Range["A" + y].EntireRow.Copy();
                   Trace.Write(ile + ",");
                   r = sh.get_Range("A2", "A2").EntireRow;
                   r.Insert(Excel.XlInsertShiftDirection.xlShiftDown);

                    if (worksheet.Cells[y, x].value != worksheet.Cells[y+1, x].value)
                    {
                        osredu=worksheet.Cells[y, x].value;
                        string plik_ = "c:\\plikixml_dzielone\\" + osredu;
                        nowy.SaveAs(plik_,
                            System.Reflection.Missing.Value,
                            System.Reflection.Missing.Value,
                            System.Reflection.Missing.Value,
                            false,
                            false,
                            Excel.XlSaveAsAccessMode.xlShared,
                            false,
                            false,
                            System.Reflection.Missing.Value,
                            System.Reflection.Missing.Value,
                            System.Reflection.Missing.Value);
                        nowy.Close();
                        worksheet.Range["A1"].EntireRow.Copy();
                        nowy = excel.Workbooks.Add();
                        sh = nowy.Sheets.Add();
                        sh.Name = "test";
                        r = sh.get_Range("A1", "A1").EntireRow;
                        r.Insert(Excel.XlInsertShiftDirection.xlShiftDown);
                        Trace.WriteLine(Environment.NewLine);
                        Trace.WriteLine(ile_szkol+ "---"+ osredu + " : " + ile);
                        Trace.WriteLine(Environment.NewLine);
                        ile = 0;
                        ile_szkol++;
                    }

                }

I mate this code. It works fine but is extremly slow and lock clipboard from using. Split over 9500 rows took one hour. Do You have any idea how improve speed of this code?

Try using OleDb driver for this task. It should be faster then automation.

string sourceConnectionString =
             @"Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0 XML;Data Source=C:\source\MyExcel.xlsx;";

using (var conn = new OleDbConnection(connectionString))
{       
    // ...
}

I am pretty sure saving o(n) times and doing worksheet.Range["A1"].EntireRow.Copy(); every time is not necessary. Can't really see what's what with some of those variable names.

However using Stopwatch , you can figure out yourself what's taking so long.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM