前幾天項(xiàng)目中有個(gè)地方需要將datagrid的數(shù)據(jù)直接導(dǎo)入excel里,以提供給用戶下載,在網(wǎng)上找了下,好像都是與下面代碼類似的實(shí)現(xiàn):
| 程序代碼: | 
| 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輸出流的形式實(shí)現(xiàn),自己嘗試下,好像可以。我裝的windowxp sp2版的,后來(lái)到同事的機(jī)器上去試,結(jié)果就出問題了,每次執(zhí)行的時(shí)候,彈出那個(gè)【打開、保存、取消】的頁(yè)面,再一點(diǎn),結(jié)果整個(gè)站點(diǎn)的頁(yè)面都關(guān)閉了,再到其他機(jī)器上去試,結(jié)果有的能正常下載,有的就不行,以前聽說(shuō)過有這么個(gè)問題,好像也沒什么好的辦法解決。
    后來(lái)考慮了下,何必不直接把寫到html流的信息直接寫到一個(gè)excel文件里面去讓客戶直接下載excel,于是稍微修改了下別人的源碼,直接生成excel文件來(lái)讓客戶下載,演示源代碼如下:
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"; //序 號(hào) 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"; //審批時(shí)間 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); } //綁定數(shù)據(jù)到datagrid1 this.datagrid1.datasource = sourcetb.defaultview; this.datagrid1.databind(); //將datagrid1構(gòu)成的html代碼寫進(jìn)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("http://")); //得到excel文件的物理地址 filepathname = filepathname +"http://" + 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(); } | 
新聞熱點(diǎn)
疑難解答
圖片精選