Introduction
In this article, I have posted the code for exporting to Excel from any data source. I have attached code and required dll in the attachment section. It is one of the ways to write with Excel and at the end of the article I posted links for the other ways to work with Excel.
Aspx code
- <asp:Button ID="btnexport_grid" runat="server" Text="Exporttogrid" OnClick="btnexport_grid_Click" />
- protected void btnexport_grid_Click(object sender, EventArgs e)
- {
- DataSet ds = ObjOSM.getboqitems();
- DataTable dt = ds.Tables[0];
- WriteExcelWithNPOI("xlsx", dt);
- }
- public void WriteExcelWithNPOI(String extension, DataTable dt)
- {
- // dll refered NPOI.dll and NPOI.OOXML
- IWorkbook workbook;
- if (extension == "xlsx")
- {
- workbook = new XSSFWorkbook();
- }
- else if (extension == "xls")
- {
- workbook = new HSSFWorkbook();
- }
- else
- {
- throw new Exception("This format is not supported");
- }
- ISheet sheet1 = workbook.CreateSheet("Sheet 1");
- //make a header row
- IRow row1 = sheet1.CreateRow(0);
- for (int j = 0; j < dt.Columns.Count; j++)
- {
- ICell cell = row1.CreateCell(j);
- String columnName = dt.Columns[j].ToString();
- cell.SetCellValue(columnName);
- }
- //loops through data
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- IRow row = sheet1.CreateRow(i + 1);
- for (int j = 0; j < dt.Columns.Count; j++)
- {
- ICell cell = row.CreateCell(j);
- String columnName = dt.Columns[j].ToString();
- cell.SetCellValue(dt.Rows[i][columnName].ToString());
- }
- }
- using (var exportData = new MemoryStream())
- {
- Response.Clear();
- workbook.Write(exportData);
- if (extension == "xlsx") //xlsx file format
- {
- Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
- Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "tpms_Dict.xlsx"));
- Response.BinaryWrite(exportData.ToArray());
- }
- else if (extension == "xls") //xls file format
- {
- Response.ContentType = "application/vnd.ms-excel";
- Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", "tpms_dict.xls"));
- Response.BinaryWrite(exportData.GetBuffer());
- }
- Response.End();
- }
- }
- Read Excel Files Using Open XML SDK In ASP.NET C#
- Read Excel Files Using Microsoft Office Interop Assemblies in ASP.NET C#
- Import and Export Excel Files into Varbinary (Max) in SQL Server
Conclusion
I hope the above information was useful. Kindly let me know your valuable feedback or thoughts.

Elango palakrishnanPosted Dec 11, 2018, 10:17 AM
Hi karthik, Will it work on multi-threading, when multiple threads trying to write its respective data table's data into different sheets in same excel work book,.
Gianluca GianxPosted Mar 24, 2018, 1:47 AM
Hi Karthik, I tried using your code. It works in my local machine but once deployed on the server, when I download the file and Excel open it, I get this message: "We found a problem with some content in 'FileName.xlsx'.Do you want us to try to recover as much as we can? If you trust the source of this workbook". I'm struggling with this problem, and I can't find any help in Internet. Do you have some idea? Have you ever encountered this error?