How can i export database table value in excel thorugh c#FKFeroz Khan10yAsked Jun 2, 2016, 6:40 AM EDTSolvedShareLinkedInXCopy linkSaveHi All, Please help me about how can i export database value in excel thorugh c#
How can i export database table value in excel thorugh c#FKFeroz Khan10yAsked Jun 2, 2016, 6:40 AM EDTSolvedShareLinkedInXCopy linkSaveHi All, Please help me about how can i export database value in excel thorugh c#
Karthik ElumalaiRank #544 · 4.5K reputation10y agoPosted Jun 7, 2016, 8:20 AM EDT✓ Accepted answer0Hi Feroz Khan,Below is the real time code which worked in my project: //button click eventprotected 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(); } }Hope this will work for you too,Kindly let me your suggestions or feedbackThanks0LDLeon DRank #851 · 2.6K reputation10y agoPosted Jun 2, 2016, 6:53 PM EDT0Hi,You can use spire.xls. Reference:http://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Data-Export-/Import-Export-Datatable-to-Excel-from-Database.html0Balaji PalaniRank #4,480 · 341 reputation10y agoPosted Jun 2, 2016, 9:43 PM EDT0Retrieve the Database values in data table :public DataTable Getdatafromexternalfile() { string strSQL = ""; DataTable myrec = new DataTable(); try { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString()); SqlCommand cmd = new SqlCommand("SELECT * FROM tblMedicaid", con); cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(); da.Fill(myrec); } catch (Exception e) { } return myrec; }After Get the datatable we can export the data table to excel sheet DataTable dataTable = Getdatafromexternalfile(); Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Document Name-Doc.xls")); Response.ContentType = "application/ms-excel"; string str = string.Empty; foreach (DataColumn dtcol in dataTable.Columns) { Response.Write(str + dtcol.ColumnName); str = "\t"; } Response.Write("\n"); foreach (DataRow dr in dt.Rows) { str = ""; for (int j = 0; j < dataTable.Columns.Count; j++) { Response.Write(str + Convert.ToString(dr[j])); str = "\t"; } Response.Write("\n"); } Response.End();0Rajeesh MenothRank #118 · 18.4K reputation10y agoPosted Jun 7, 2016, 8:51 PM EDT0Hi, You can choose “Spire.Xls” Or Other third party libraries. Check the following reference : http://www.c-sharpcorner.com/UploadFile/83fe73/solutions-to-export-data-from-database-to-excel-in-C-Sharp/0Vinay SinghRank #604 · 4K reputation10y agoPosted Jun 8, 2016, 2:17 AM EDT0 Hi have a look of this linkhttp://www.aspsnippets.com/Articles/Export-ASPNet-Web-Page-with-images-to-PDF-using-ITextsharp.aspx0Join to answer this questionEvery answer you publish earns 20 SHARP.Create free accountSign inKnow the answer?Answer a question in the forums and earn +20 SHARP for every answer.See unanswered questionsForumsTop experts01Arun PottiAnswers in the forums20answers02Tahir AnsariAnswers in the forums5answers03Kamlesh KumarAnswers in the forums5answers04Sam HobbsAnswers in the forums4answers05Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.JoinFull leaderboard →2026DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
LDLeon DRank #851 · 2.6K reputation10y agoPosted Jun 2, 2016, 6:53 PM EDT0Hi,You can use spire.xls. Reference:http://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Data-Export-/Import-Export-Datatable-to-Excel-from-Database.html0
Balaji PalaniRank #4,480 · 341 reputation10y agoPosted Jun 2, 2016, 9:43 PM EDT0Retrieve the Database values in data table :public DataTable Getdatafromexternalfile() { string strSQL = ""; DataTable myrec = new DataTable(); try { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString()); SqlCommand cmd = new SqlCommand("SELECT * FROM tblMedicaid", con); cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(); da.Fill(myrec); } catch (Exception e) { } return myrec; }After Get the datatable we can export the data table to excel sheet DataTable dataTable = Getdatafromexternalfile(); Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Document Name-Doc.xls")); Response.ContentType = "application/ms-excel"; string str = string.Empty; foreach (DataColumn dtcol in dataTable.Columns) { Response.Write(str + dtcol.ColumnName); str = "\t"; } Response.Write("\n"); foreach (DataRow dr in dt.Rows) { str = ""; for (int j = 0; j < dataTable.Columns.Count; j++) { Response.Write(str + Convert.ToString(dr[j])); str = "\t"; } Response.Write("\n"); } Response.End();0
Rajeesh MenothRank #118 · 18.4K reputation10y agoPosted Jun 7, 2016, 8:51 PM EDT0Hi, You can choose “Spire.Xls” Or Other third party libraries. Check the following reference : http://www.c-sharpcorner.com/UploadFile/83fe73/solutions-to-export-data-from-database-to-excel-in-C-Sharp/0
Vinay SinghRank #604 · 4K reputation10y agoPosted Jun 8, 2016, 2:17 AM EDT0 Hi have a look of this linkhttp://www.aspsnippets.com/Articles/Export-ASPNet-Web-Page-with-images-to-PDF-using-ITextsharp.aspx0
Karthik ElumalaiRank #544 · 4.5K reputationPosted Jun 7, 2016, 8:20 AM EDT
Leon DRank #851 · 2.6K reputationPosted Jun 2, 2016, 6:53 PM EDT
Balaji PalaniRank #4,480 · 341 reputationPosted Jun 2, 2016, 9:43 PM EDT
Rajeesh MenothRank #118 · 18.4K reputationPosted Jun 7, 2016, 8:51 PM EDT
Vinay SinghRank #604 · 4K reputationPosted Jun 8, 2016, 2:17 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts