ASP.NET 打印、导出 【转载】

我是菜鸟,我是新手。前几天写程序时,应客户要求加上打印和数据导出功能,在过去从未做过类似的,就在网上搜了一搜,结果云云,方法云云。试过好多都要出错。于是乎,拿过来好几个,相比对照之下,终于弄出来了,记录下来,以后会用得着,也给予同我一样的新手借鉴。

导出功能,还是并非我最想要的(怎么能把浏览器下载那个提示框取消了,就如同保存图片一样),有高手路过,请指点一二......

 

 

************导出GridView数据************

 

1、页面中添加:

<asp:LinkButton ID="lbtn_Excel" runat="server" OnClick="lbtn_Excel_Click"><img src="../icon/excel.jpg" height="20" align="absmiddle" border="0" />导出</asp:LinkButton>

 

2、后置代码,lbtn_Excel_Click方法:

    protected void lbtn_Excel_Click(object sender, EventArgs e)     {         string FileName = "MateOutList(" + DateTime.Now.ToShortDateString()+"_"          +DateTime.Now.ToShortTimeString() + ").xls";         //导出Excel         Response.Clear();         Response.AddHeader("content-disposition", "attachment;filename=" + FileName);         Response.Charset="GB2312";         Response.ContentType="application/vnd.xls";         Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");         System.IO.StringWriter strW=new System.IO.StringWriter();         System.Web.UI.HtmlTextWriter htmlTW=new HtmlTextWriter(strW);         //GridView去样式         System.Drawing.Color oldbackcolor = gv_Excel.HeaderStyle.BackColor;         System.Drawing.Color oldforecolor = gv_Excel.HeaderStyle.ForeColor;

 

        GridView1.RenderControl(htmlTW);         Response.Write(strW.ToString());         Response.End();     }

 

 

3、切记重写 VerifyRenderingInServerForm方法

 

    public override void VerifyRenderingInServerForm(Control control)     {         // Confirms that an HtmlForm control is rendered for     }

 

 

4、如果LinkButton是在UpdatePanel<ContentTemplate></ContentTemplate>之间,切记在触发器中写上去<asp:PostBackTrigger ControlID="lbtn_Excel" />,如:

 

    <Triggers>          <asp:PostBackTrigger ControlID="lbtn_Excel" />     </Triggers>

 

 

 

*************OK,原来导出就是如此简单。

 

 

 

**********打印************

1、页面添加:

<asp:LinkButton ID="lbtn_Print" runat="server" OnClientClick="return PintPage();"><img src="../icon/print.jpg" height="20" align="absmiddle" border="0" />打印</asp:LinkButton>

 

<script language="javascript" type="text/javascript">

function PintPage() {

      //指向打印页面       window.showModalDialog('PrintMateOut.aspx?MainID='+ document.getElementById('hid_billid').value+'&SubID='+document.getElementById('hid_sub_id').value,'','dialogHeight:375px;dialogWidth:740px;help:0;');

}

 

</script>

 

2、打印页面,后置代码并没什么东西,前台用JavaScript处理了: <meta http-equiv="Content-Type" content="text/html; charset=gb2312"></meta>

<script language="javascript" type="text/javascript">

 

        function doPage()        {             layLoading.style.display = "none";        }         //设置网页打印的页眉页脚为空         function PageSetup_Null()         {              try              {               var Wsh=new ActiveXObject("WScript.Shell");               HKEY_Key="header";               Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");               HKEY_Key="footer";               Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");                }              catch(e){}         }                //设置网页打印的页眉页脚为默认值         function  PageSetup_Default()         {            try          {           var Wsh=new ActiveXObject("WScript.Shell");           HKEY_Key="header";           Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&w&b页码,&p/&P");           HKEY_Key="footer";           Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&u&b&d");          }          catch(e){}         }

                 /*打印函数          *doType 页面页脚类型  0 设置为空,1 设置为默认          */         function PrintTable(doType)   //打印函数         {                 if(doType == '0')              {                 PageSetup_Null();              }             else             {                 PageSetup_Default();              }              document.getElementById("btn_Print").style.display = "none";              document.getElementById("btn_NoPrint").style.display = "none";              window.print();              document.getElementById("btn_Print").style.display = "";              document.getElementById("btn_NoPrint").style.display = "";              window.location.reload();         } 

<body>

<!-------------------------要打印部分------------------------

<table>

    要打印的表格

</table>

-------------------------要打印部分------------------------->

 

<table cellpadding="0" cellspacing="0" border="0" style="width: 666px;">                 <tr>                     <td align="right" style="height: 27px;">                         <asp:Button ID="btn_Print" runat="server" Text="确认打印" OnClientClick="PrintTable('0')"                             Height="25px" Width="80px" />&nbsp;&nbsp;&nbsp;                         <asp:Button ID="btn_NoPrint" runat="server" Text="取消打印" OnClientClick="JavasCript:window.close();"                             Height="25px" Width="80px" />                     </td>                 </tr>             </table>

 

</body>

 

 

*************OK,打印也就此搞定。

 

如果有疑问或着有改进,可给我留言,呵呵,共同探讨,共同进步。

 

 

posted @ 2012-04-30 20:19  江宁织造  阅读(457)  评论(0编辑  收藏  举报