I tried to export the dataset which have 4 tables to the excel sheet, Unfortunately I can't. I have code to export data table to excel. So instead of dataset, I called the "ExportToExcel" function which I have in my code to export datatable to excel 4 times. But once it created the first sheet, it stops the control flow. Control doesn't call the second function ("ExportToExcel(dsResult.Tables[2], "AthleteSentCount");") Here is the code
protected void ExportToExcel(object sender, EventArgs e)
{
ExportToExcel(dsResult.Tables[3], "AthleteInboxCount");
ExportToExcel(dsResult.Tables[2], "AthleteSentCount");
ExportToExcel(dsResult.Tables[0], "CoachInboxCount");
ExportToExcel(dsResult.Tables[1], "CoachSentCount");
}
void ExportToExcel(DataTable dt, string FileName)
{
if (dt.Rows.Count > 0)
{
string filename = FileName + ".xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition",
"attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
If any body knows to export the dataset to the excel with the bar chart please help me. Else please give your solution to the problem.
First of all your function start streaming content
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
to end user and END it with
Response.End();
Moreover if you fix this your code will product 4 excel files.
There is an existing code for creating Excel sheets on stack overflow: How to add additional worksheets to an Excel from DataTable The only change you will have to made will be saving xlsx to stream and transmit this stream to user.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With