LisBbox items values download to Excel Sheet in asp.net c#
Listbox
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ashok Yadav184 reputationPosted Dec 2, 2014, 1:28 AM EST
protected void btnExportfromDt_Click(object sender, EventArgs e) { DataTable dtplants = new DataTable(); dtplants.Columns.Add("Plants"); foreach (ListItem li in lbxPlants.Items) { DataRow drplants = dtplants.NewRow(); drplants[0] = li.Value; dtplants.Rows.Add(drplants); } string strFilename = "EmpDetails.xls"; UploadDataTableToExcel(dtplants , strFilename); } protected void UploadDataTableToExcel(DataTable dtEmp,string filename) { string attachment = "attachment; filename="+filename; Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/vnd.ms-excel"; string tab = string.Empty; foreach (DataColumn dtcol in dtEmp.Columns) { Response.Write(tab + dtcol.ColumnName); tab = "\t"; } Response.Write("\n"); foreach (DataRow dr in dtEmp.Rows) { tab = ""; for (int j = 0; j < dtEmp.Columns.Count; j++) { Response.Write(tab + Convert.ToString(dr[j])); tab = "\t"; } Response.Write("\n"); } Response.End(); }