EPPlus is a very helpful open-source 3rd party dll for exporting data to excel. To implement the same in you project, follow the below steps:
- Download EPPlus.dll. f
- Add reference to EPPlus.dll in your project
- Use the following Class:
- using OfficeOpenXml;
- using OfficeOpenXml.Drawing;
- using OfficeOpenXml.Style;
- public class clsGeneral
- {
- public void GenerateExcel2007(string p_strPath, DataSet p_dsSrc)
- {
- using (ExcelPackage objExcelPackage = new ExcelPackage())
- {
- foreach (DataTable dtSrc in p_dsSrc.Tables)
- {
- //Create the worksheet
- ExcelWorksheet objWorksheet = objExcelPackage.Workbook.Worksheets.Add(dtSrc.TableName);
- //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
- objWorksheet.Cells["A1"].LoadFromDataTable(dtSrc, true);
- objWorksheet.Cells.Style.Font.SetFromFont(new Font("Calibri", 10));
- objWorksheet.Cells.AutoFitColumns();
- //Format the header
- using (ExcelRange objRange = objWorksheet.Cells["A1:XFD1"])
- {
- objRange.Style.Font.Bold = true;
- objRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
- objRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
- objRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
- objRange.Style.Fill.BackgroundColor.SetColor(Color.FromA#eaeaea);
- }
- }
- //Write it back to the client
- if (File.Exists(p_strPath))
- File.Delete(p_strPath);
- //Create excel file on physical disk
- FileStream objFileStrm = File.Create(p_strPath);
- objFileStrm.Close();
- //Write content to excel file
- File.WriteAllBytes(p_strPath, objExcelPackage.GetAsByteArray());
- }
- }
- }
- Call the function from any page of your application with appropriate details.

Elango palakrishnanPosted Nov 27, 2018, 7:28 AM
Hi jain , I need to read and write the excel sheet by using multiple threads in my automation framework. Does EPPlus supports n number of threads access the same excel file at same time, Can we achieve this ? Or shall i need to look other ideas?
satish kumarPosted Jul 18, 2018, 9:10 AM
For EPPlus is there any limitation like Upto certain rows and columns or file size
Carlo PeñaPosted Dec 18, 2017, 1:26 AM
Thanks for this tutorial. But I have a problem. I'm using this in ASP.NET and C#, My problem is when i export the excel file in a location it exports in the server unit not in my personal computer
Ashish PandavPosted Oct 9, 2017, 1:42 AM
Specified cast is not valid. public void GenerateExcel2007(DataSet p_dsSrc) { try { string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); string pathDownload = Path.Combine(pathUser, "Downloads\\"); string p_strPath = pathDownload + "WarehouseStockDetailReport_" + Convert.ToString(DateTime.Now.Month) + "-" + Convert.ToString(DateTime.Now.Day) + "-" + Convert.ToString(DateTime.Now.Year) + "-T" + Convert.ToString(DateTime.Now.Hour) + "-" + Convert.ToString(DateTime.Now.Minute) + "-" + Convert.ToString(DateTime.Now.Second) + Convert.ToString(DateTime.Now.Millisecond) + ".xls"; using (ExcelPackage objExcelPackage = new ExcelPackage()) { foreach (DataTable dtSrc in p_dsSrc.Tables) { //Create the worksheet ExcelWorksheet objWorksheet = objExcelPackage.Workbook.Worksheets.Add(dtSrc.TableName); //dtSrc.Rows[0][0] = "This is new file!"; objWorksheet.Cells["A1"].LoadFromDataTable(dtSrc, true); objWorksheet.Cells.AutoFitColumns(); using (ExcelRange objRange = objWorksheet.Cells["A1:XFD1"]) { objRange.Style.Font.Bold = true; objRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; objRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center; objRange.Style.Fill.PatternType = ExcelFillStyle.LightGray; } } //Write it back to the client if (File.Exists(p_strPath)) File.Delete(p_strPath); //Create excel file on physical disk FileStream objFileStrm = File.Create(p_strPath); objFileStrm.Close(); //Write content to excel file File.WriteAllBytes(p_strPath, objExcelPackage.GetAsByteArray()); return; } } catch (Exception ex) { } }
ras rasPosted Mar 29, 2017, 2:39 AM
It is not working after hosting can u help me? var pck = new OfficeOpenXml.ExcelPackage();pck.Load(File.OpenRead("~\\EXCELS\\Book2.xlsx"));
ras rasPosted Mar 29, 2017, 2:38 AM
Var pck = new OfficeOpenXml.ExcelPackage();pck.Load(File.OpenRead("~\\EXCELS\\Book2.xlsx"));
Carolina MontesPosted Dec 26, 2016, 4:48 PM
I want to export 3,000,000 records with EPPlus, but i cant, help me please.
Parth MehtaPosted Nov 22, 2016, 9:28 AM
File.WriteAllBytes(p_strPath, objExcelPackag-e.GetAsByteArray()); in this line what dose 'E' means. can you please help.
Venkat SubramaniyamPosted Jul 14, 2016, 8:19 AM
Thank you !!!!
Zeeshan AzimPosted May 20, 2015, 7:48 AM
Thank you very much Punit !
Imtiyaz KhanPosted Mar 2, 2015, 12:51 AM
I feel RenderControl() is the best alternative for exporting data to excel file using response object.