Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

Thanks in advance.

share|improve this question
 
what issue u r getting in this? –  SANDEEP Oct 15 at 6:00
 
Thanks for your response. Control stops its operation once it creates its first excel sheet ie once the "ExportToExcel(dsResult.Tables[3], "AthleteInboxCount");" function got excecuted –  Arun Kumar T Oct 15 at 6:01
 
ys why not?try to pass datatabe in a loop till Data Set length –  SANDEEP Oct 15 at 6:03
 
I tried, but it stops the flow after "Response.End();" method while executing the first loop –  Arun Kumar T Oct 15 at 6:11
 
remove Response.End(). it will work –  SANDEEP Oct 15 at 6:15
show 1 more comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
 
Thank you @Garath, your post is very useful to me. I have to made some little changes like syntax. But it works great. Again thanks. –  Arun Kumar T Oct 15 at 7:25
 
Can I know why the control stops when it creates the first sheet in the above code? –  Arun Kumar T Oct 15 at 7:26
 
@ArunKumarT the code not excatly stops, but because you call Response.End(); the rest of code is not send to end user. –  Garath Oct 15 at 7:31
 
Thank you. Garth –  Arun Kumar T Oct 15 at 7:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.