asp.net导出数据生成真正的excel文件
发布网友
发布时间:2022-04-22 01:36
我来回答
共2个回答
热心网友
时间:2022-04-27 13:14
我一般都是用gridview导出数据,你看看代码吧 对你有没有帮助, ----------------------按钮事件-------------------protected void Button2_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "sssss.xls");
} private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
// turn off paging
GridView1.AllowPaging = false;
this.GridView1.DataBind(); GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End(); // turn the paging on again
GridView1.AllowPaging = true;
this.GridView1.DataBind(); } 下面这句不加的话回出错误 public override void VerifyRenderingInServerForm(Control control)
{ }页面:EnableEventValidation = "false"
热心网友
时间:2022-04-27 14:32
楼主是想将数据库中的数据字段,读出来,存到excel文档?还是想直接读出就是excel文档?对于前者,我可以解决;后者我只是有点设计思想,目前还没实现