您的位置:首页 > Web前端 > HTML

以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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: