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,打印也就此搞定。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值