首页 > 编程 > .NET > 正文

将ASP.NET页面内的数据导出到Excel 或 Word中

2024-07-10 13:10:10
字体:
来源:转载
供稿:网友

在以下按钮单击事件中实现:
private void btnmime_click(object sender, system.eventargs e)
{
 binddata();
 response.contenttype = "application/vnd.ms-excel";
 response.addheader("content-disposition", "inline;filename="
   +   httputility.urlencode("下载文件.xls",encoding.utf8   )   );  
 

 //如果输出为word,修改为以下代码
 //response.contenttype = "application/ms-word"
 //response.addheader("content-disposition", "inline;filename=test.doc")
 stringbuilder sb=new stringbuilder();
 system.io.stringwriter sw = new system.io.stringwriter(sb);
 system.web.ui.htmltextwriter hw = new system.web.ui.htmltextwriter(sw);
 sb.append("<html><body>");
 dgshow.rendercontrol(hw);
 sb.append("</body></html>");
 response.write(sb.tostring());
 response.end();
}
注:1.若datagrid中有按钮列,则在导出前应先将其隐藏.
    2.若datagrid有分页,而又要打印所有数据的话就应先取消分页.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表