Click here to Skip to main content
16,017,502 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have data more that 1 lakh which i need to export in excel. So when i am exporting data its taking too much time to export for large data :(

and if i export 500 to 1000 records it export soon but not more than that.

Please help me to export data soon and fast for 1 lakh records

What I have tried:

string _path = "C:\\Test.xls";

 DataSet ds = new DataSet();
 ds.Tables.Add(dt);    \\loading datatable "dt" into dataset

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;

                xlWorkBook = xlApp.Workbooks.Add(misValue);
                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                xlWorkSheet.Cells[1, 1] = "Sheet 1 content";

                xlWorkBook.SaveAs(_path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);

                ExportDataSetToExcel(ds, _path);

 private void ExportDataSetToExcel(DataSet ds, string _filename)
         {
             //Creae an Excel application instance
             Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

             //Create an Excel workbook instance and open it from the predefined location
             Microsoft.Office.Interop.Excel.Workbook excelWorkBook = excelApp.Workbooks.Open(_filename);

             foreach (DataTable table in ds.Tables)
             {
                 //Add a new worksheet to workbook with the Datatable name
                 Microsoft.Office.Interop.Excel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add();
                 excelWorkSheet.Name = table.TableName;

                 for (int i = 1; i < table.Columns.Count + 1; i++)
                 {
                     excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
                 }

                 for (int j = 0; j < table.Rows.Count; j++)
                 {
                    
                     for (int k = 0; k < table.Columns.Count; k++)
                     {
                         excelWorkSheet.Cells[j + 2, k + 1] = table.Rows[j].ItemArray[k].ToString();
                     }
                 }
             }

             excelWorkBook.Save();
             excelWorkBook.Close();
             excelApp.Quit();

         }
Posted
Updated 3-May-16 23:33pm
v3

 
Share this answer
 
Comments
[no name] 28-Apr-16 7:24am    
I did not find any solution to export fast and quickly from these above links please help
VR Karthikeyan 28-Apr-16 7:28am    
Have you tried EPPlus?
[no name] 28-Apr-16 7:30am    
Yes i tried :(
VR Karthikeyan 28-Apr-16 7:44am    
I have used EPPlus before, it was very fast with my huge data (75 columns, 1 Lakh rows. Actually EPPlus speeds up exporting huge data than Excel Interop. Try exporting data as ExcelRange, instead of writing cell by cell. Also check this link. http://www.codeproject.com/Tips/659666/Export-very-large-data-to-Excel-file
I agree with VR Karthikeyan, EPPlus is a good choice. Anyway, you can go through and look into below links: Operate excel document in C#--Import/Export Data from DataTable to Excel or A free "Export to Excel" C# class, using OpenXML
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900