前几天项目中有个地方需要将datagrid的数据直接导入excel里,以提供给用户下载,在网上找了下,好像都是与下面代码类似的实现:
程序代码: |
this.enableviewstate = false; system.globalization.cultureinfo mycitrad = new system.globalization.cultureinfo("zh-cn",true); system.io.stringwriter ostringwriter = new system.io.stringwriter(mycitrad); system.web.ui.htmltextwriter ohtmltextwriter = new system.web.ui.htmltextwriter(ostringwriter); this.datagrid1.rendercontrol(ohtmltextwriter); response.write(ostringwriter.tostring()); response.end(); |
原理也就是把datagrid的信息以流的形式写到html输出流的形式实现,自己尝试下,好像可以。我装的windowxp sp2版的,后来到同事的机器上去试,结果就出问题了,每次执行的时候,弹出那个【打开、保存、取消】的页面,再一点,结果整个站点的页面都关闭了,再到其他机器上去试,结果有的能正常下载,有的就不行,以前听说过有这么个问题,好像也没什么好的办法解决。
后来考虑了下,何必不直接把写到html流的信息直接写到一个excel文件里面去让客户直接下载excel,于是稍微修改了下别人的源码,直接生成excel文件来让客户下载,演示源代码如下:
webform4.aspx----html部分:
程序代码: |
<%@ page language="c#" codebehind="webform4.aspx.cs" autoeventwireup="false" inherits="webui.webform4" %> <!doctype html public "-//w3c//dtd html 4.0 transitional//en" > <html> <head> <title>webform4</title> <meta name="generator" content="microsoft visual studio .net 7.1"> <meta name="code_language" content="c#"> <meta name="vs_defaultclientscript" content="javascript"> <meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5"> </head> <body ms_positioning="gridlayout"> <form id="form1" method="post" runat="server"> <asp:datagrid id="datagrid1" runat="server" width="100%"> <itemstyle horizontalalign="center"></itemstyle> <headerstyle horizontalalign="center"></headerstyle> <footerstyle horizontalalign="center"></footerstyle> <pagerstyle pagebuttoncount="15" mode="numericpages"></pagerstyle> </asp:datagrid> <asp:button id="button1" runat="server" text="button"></asp:button> </form> </body> </html> |
程序代码: |
private void button1_click(object sender, system.eventargs e) { datatable sourcetb = new datatable(); datacolumn mydatacolumn; mydatacolumn = new datacolumn(); mydatacolumn.datatype = system.type.gettype("system.string"); mydatacolumn.columnname = "rowindex"; //序 号 sourcetb.columns.add(mydatacolumn); mydatacolumn = new datacolumn(); mydatacolumn.datatype = system.type.gettype("system.string"); mydatacolumn.columnname = "checkupmanname"; //审批人 sourcetb.columns.add(mydatacolumn); mydatacolumn = new datacolumn(); mydatacolumn.datatype = system.type.gettype("system.string"); mydatacolumn.columnname = "checkupideas"; //审批意见 sourcetb.columns.add(mydatacolumn); mydatacolumn = new datacolumn(); mydatacolumn.datatype = system.type.gettype("system.string"); mydatacolumn.columnname = "checkupdate"; //审批时间 sourcetb.columns.add(mydatacolumn); mydatacolumn = new datacolumn(); mydatacolumn.datatype = system.type.gettype("system.string"); mydatacolumn.columnname = "checkuprole"; //审批岗位 sourcetb.columns.add(mydatacolumn); mydatacolumn = new datacolumn(); mydatacolumn.datatype = system.type.gettype("system.string"); mydatacolumn.columnname = "handletype"; //操作类型(1:提交| 9:驳回) sourcetb.columns.add(mydatacolumn); datarow mydatarow; for(int i = 0;i < 30;i ++) { mydatarow = sourcetb.newrow(); mydatarow["rowindex"] = i.tostring(); mydatarow["checkupmanname"] = "张三"; mydatarow["checkupideas"] = "同意"; mydatarow["checkupdate"] = "2006-03-20"; mydatarow["checkuprole"] = "物资部主任"; sourcetb.rows.add(mydatarow); } //绑定数据到datagrid1 this.datagrid1.datasource = sourcetb.defaultview; this.datagrid1.databind(); //将datagrid1构成的html代码写进stringwriter this.datagrid1.page.enableviewstate = false; system.io.stringwriter tw = new system.io.stringwriter(); system.web.ui.htmltextwriter hw = new system.web.ui.htmltextwriter(tw); this.datagrid1.rendercontrol(hw); string htmlinfo = tw.tostring().trim(); string docfilename = "审批信息.xls"; string filepathname = request.physicalpath; filepathname = filepathname.substring(0,filepathname.lastindexof("//")); //得到excel文件的物理地址 filepathname = filepathname +"//" + docfilename; system.io.file.delete(filepathname); filestream fs = new filestream(filepathname, filemode.create); binarywriter bwriter= new binarywriter(fs,system.text.encoding.getencoding("gb18030")); //将datagrid的信息写入excel文件 bwriter.write(htmlinfo); bwriter.close(); fs.close(); } |
新闻热点
疑难解答