出口数据集Excel表格在asp.net C# [英] Export dataset to the excel sheet in asp.net c#

查看:166
本文介绍了出口数据集Excel表格在asp.net C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着要导出的有4个表Excel工作表的数据集,可惜我做不到。我有code导出数据表到Excel。因此,而不是数据集,我称之为ExportToExcel功能,我有我的code出口数据表到excel的4倍。但一旦它创建的第一片材​​,它停止的控制流程。控制不会调用第二个函数(ExportToExcel(dsResult.Tables [2],AthleteSentCount);)这里是code

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();
    }
}

如果任何机构都知道的数据导出到Excel的条形图请帮助我。否则,请给你的解决问题的方法。

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();

此外,如果你解决这个问题你code意志产品4 Excel文件。

Moreover if you fix this your code will product 4 excel files.

有一个现有的code对堆栈溢出创建Excel表:<一href=\"http://stackoverflow.com/questions/12980640/how-to-add-additional-worksheets-to-an-excel-from-datatable\">How从数据表增加额外的工作表到Excel
你将不得不做出的唯一改变将节能XLSX流和该流传输给用户。

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.

这篇关于出口数据集Excel表格在asp.net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆