当前位置:编程学习 > C#/ASP.NET >>

asp.net把数据库记录导入成excel文件

asp教程.net把数据库教程记录导入成excel文件
这是一个简单的导入excel文件代码,利用exprot.aspx导出指定数据库中的记录成xls文件哦。

protected void button2_click(object sender, eventargs e)
  {
  response.redirect("export.aspx?search=" + txtsearch.text);
  }
 %>

export.aspx
获取一个datatable,
在前台的div里直循环datatable:
<div runat="server" id="exceldiv">
  <table>
</div>


后台:
<%
  if (!ispostback)
  {
  export("报表");
  }

  private void export(string filename)
  {
  response.clear();
  response.addheader("content-disposition", "attachment;filename=" + filename + ".xls");
  response.charset = "gb2312";
  response.contenttype = "application/vnd.xls";
  response.contentencoding = system.text.encoding.getencoding("gb2312");
  system.io.stringwriter stringwrite = new system.io.stringwriter();
  system.web.ui.htmltextwriter htmlwrite = new htmltextwriter(stringwrite);
  exceldiv.rendercontrol(htmlwrite);
  response.write(stringwrite.tostring());
  response.end();
  }

补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,