以HTML格式导出Excel报表
2011-03-17 19:13
274 查看
Hi man, welcome to to this example of creating a sample report using C#.
In this artical, it will cover how to export a report to a excel file and how to farmat the data came from a sql database.
Below pic is the screen shot of this example.
View Code
protectedvoid btnExport_Click(object sender, EventArgs e)
{
string attachment ="attachment; filename= Test Report"+ DateTime.Now +".xls";
if (GridView1.Rows.Count +1<65536){
GridView1.AllowPaging =false;
StringWriter sw =new StringWriter();
HtmlTextWriter htw =new HtmlTextWriter(sw);
Response.ContentType ="";
HtmlForm frm =new HtmlForm();
//start the excel output
Response.ContentType ="application/vnd.ms-excel";
Response.AddHeader("content-disposition", attachment);
Response.Charset ="";
Controls.Add(frm);
frm.Controls.Add(GridView1);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
else {
Response.Write("<script>alert('Too many rows!')</script>");
}
}
Now, have you got the correct result? Help this article will help you. :)
If you want to know how to format your data, pls go to this link: http://www.cnblogs.com/Dannier/archive/2011/03/09/1978632.html
In this artical, it will cover how to export a report to a excel file and how to farmat the data came from a sql database.
Below pic is the screen shot of this example.
View Code
protectedvoid btnExport_Click(object sender, EventArgs e)
{
string attachment ="attachment; filename= Test Report"+ DateTime.Now +".xls";
if (GridView1.Rows.Count +1<65536){
GridView1.AllowPaging =false;
StringWriter sw =new StringWriter();
HtmlTextWriter htw =new HtmlTextWriter(sw);
Response.ContentType ="";
HtmlForm frm =new HtmlForm();
//start the excel output
Response.ContentType ="application/vnd.ms-excel";
Response.AddHeader("content-disposition", attachment);
Response.Charset ="";
Controls.Add(frm);
frm.Controls.Add(GridView1);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
else {
Response.Write("<script>alert('Too many rows!')</script>");
}
}
Now, have you got the correct result? Help this article will help you. :)
If you want to know how to format your data, pls go to this link: http://www.cnblogs.com/Dannier/archive/2011/03/09/1978632.html
相关文章推荐
- Firefox导出Excel报表为Html格式
- Spring MVC+ Ireport 做统计报表,导出HTML、EXCEL等格式的文件,打印;
- Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,html 格式
- c#使用输出html字符串进行导出excel功能时,常用到得几种格式:
- 水晶报表导出为word,excel和pdf格式
- 水晶报表导出为word,excel和pdf格式
- 巧用asp导出csv格式excel报表
- Java导出Excel格式的统计报表
- 用 HTML 格式导出 Excel 时,如何保留显示网格线
- 用 HTML 格式导出 Excel 时,如何保留显示网格线
- JAVA将Excel中的报表导出为图片格式(三)换一种实现
- 利用html导出excel的格式问题
- 用 HTML 格式导出 Excel 时,如何保留显示网格线
- html导出到excel数据格式不正确解决方法
- 水晶报表导出为word,excel和pdf格式(转载)
- JSP应用导出Excel报表的简单实现以及中文乱码彻底解决(HTML)(转)
- 使用jasperreports制作报表(导出pdf excel html)
- jasperreports制作报表(导出pdf excel html)
- JSP应用导出Excel报表的简单实现以及中文乱码彻底解决(HTML)
- jasperreport实现Html、Pdf、Rtf、Excel、Xml报表导出