实现网络打印的办法

 
实现网络打印的办法
1、 IE打印
缺点:原文件的打印格式比较固定
优点:减轻了服务器的工作负荷
1) 利用JS实现只打印想打印的部分
<script language="javascript">
        function preview()
        {
        bdhtml=window.document.body.innerHTML;
        sprnstr="<!--startprint-->";
        eprnstr="<!--endprint-->";
        prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
        prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
        window.document.body.innerHTML=prnhtml;
        window.print();
        }
        </script>
<!--startprint-->
 打印的部分(DataView控件)
<!--endprint-->
<input type="button" name="print" value="预览并打印" οnclick="preview()">
         2) 利用IE隐藏不打印的部分
            <style>
@media Print { .Noprn { DISPLAY: none }}
                     </style>
            <p class="Noprn">不打印得部分</p>
            <input class="Noprn" type="button" οnclick="window.print()" value="print">
2、 利用WebBrower控件打印
缺点:增加了服务的负担
优点:格式比较灵活
 
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
namespace ASPNETPrint
{
       /// <summary>
       /// PrintClass 的摘要说明。
       /// </summary>
       public class PrintClass
       {
              public PrintClass()
              {
                     //
                     // TODO: 在此处添加构造函数逻辑
                     //
              }
              /// <summary>
              /// 拼出要打印的表
              /// </summary>
              /// <param name="ds">DATASET</param>
              /// <param name="dg">DATAGRID</param>
              /// <returns></returns>
              public string DGPrint(DataSet ds)
              {
                     //*************************************************************//
 
                     DataTable myDataTable=new DataTable();
                     myDataTable=ds.Tables[0];
 
                     int myRow=myDataTable.Rows.Count;
                     int myCol=myDataTable.Columns.Count;
 
                     StringBuilder sb=new StringBuilder(); 
    
                     string colHeaders="<html><body>"+
                            "<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'VIEWASTEXT></object>"
                            +"<table><tr>"; 
 
                     for(int i=0;i<myCol;i++)
                     {   
               //显示列名
                            colHeaders +="<td>"+ myDataTable.Columns[i].ColumnName.ToString()+"</td>";
                     }
                     colHeaders += "</tr>";
                     sb.Append(colHeaders);
 
                     for(int i=0;i<myRow;i++)
                     {      
                            sb.Append("<tr>");
                            for(int j=0;j<myCol;j++)
                            {
                                   sb.Append("<td>");
                    //显示每一行每一列的数据
                                   sb.Append(myDataTable.Rows[i][j].ToString().Trim());
                                   sb.Append("</td>");
                            }
                            sb.Append("</tr>");  
                     }
 
                     sb.Append("</table></body></html>");
                     colHeaders=sb.ToString();
           //打印              
                     colHeaders+="<script languge='javascript'>WebBrowser.ExecWB(6,1); window.opener=null;window.close();</script>" ;
                     return(colHeaders);
                     //*************************************************************//
  
              }
 
              /// <summary>
              /// 拼出要打印的表
              /// </summary>
              /// <param name="dg">datagrid</param>
              /// <returns></returns>
              public string DGPrint(DataGrid dg)
              {
                     int myRow=dg.Items.Count;  
                     int myCol=dg.Columns.Count;
                     StringBuilder sb=new StringBuilder();          
 
                     string colHeaders="<html><body>"+
                            "<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'VIEWASTEXT></object>"
                            +"<table><tr>"; 
 
                     for(int i=0;i<myCol;i++)
                     {
               //显示DataGrir的列头
                            colHeaders +="<td>"+ dg.Columns[i].HeaderText.ToString()+"</td>";
                     }
                     colHeaders += "</tr>";
                     sb.Append(colHeaders);
 
                     for(int i=0;i<myRow;i++)
                     {      
                            sb.Append("<tr>");
                            for(int j=0;j<myCol;j++)
                            {
                                   sb.Append("<td>");
                    //显示数据
                                   sb.Append(dg.Items[i].Cells[j].Text.Trim());
                                   sb.Append("</td>");
                            }
                            sb.Append("</tr>");  
                     }
                     sb.Append("</table></body></html>");
                     colHeaders=sb.ToString();
           打印并预览               
                     colHeaders+="<script languge='javascript'>WebBrowser.ExecWB(7,1); window.opener=null;window.close();</script>" ;
                     return(colHeaders);
              }
 
       }
}
利用服务器段的控件btnPrint
private void btnPrint_Click(object sender, System.EventArgs e)
              {
                     PrintClass myP = new PrintClass();
                     Response.Write(myP.DGPrint(Bind()));
 
              }
3、 导出到Excel或Word然后再打印
缺点:客户端必须安装Excel,World等,并且客户端有运行服务器端Office的权利和服务器有写入客户端文件夹文件的权利,因为文件是保存在客户端的。
优点:格式比较灵活,你可以对导出的数据进行进一步的设计(总数,平均等)
实现:一般要求保存的数据,和格式不固定单据
1) 利用DataGrid
    private void btnMIME_Click(object sender, System.EventArgs e)
        {
               BindData();
 
               Response.ContentType = "application/vnd.ms-excel";
               Response.AddHeader("Content-Disposition", "inline;filename="
                      +   HttpUtility.UrlEncode("下载文件.xls",Encoding.UTF8    )   );  
 
               //如果输出为Word,修改为以下代码
               //Response.ContentType = "application/ms-word"
               //Response.AddHeader("Content-Disposition", "inline;filename=test.doc")
               StringBuilder sb=new StringBuilder();
               System.IO.StringWriter sw = new System.IO.StringWriter(sb);
              System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
               sb.Append("<html><body>");
        //前台的DataGrid
               dgShow.RenderControl(hw);
               sb.Append("</body></html>");
               Response.Write(sb.ToString());
               Response.End();
        }
2) 利用DataSet,在项目中必须导入Excel的控件的类库
   private void btnCom_Click(object sender, System.EventArgs e)
        {
        DateSet ds= BindData();
        strExcelFileName= Server.MapPath("ComExcel.xls");
            if (ds.Tables.Count==0 || strExcelFileName=="")
{
Return ;
}
Else
{
 Excel.Application excel= new Excel.Application();
                   int rowIndex=1;
                   int colIndex=0;
                   excel.Application.Workbooks.Add(true);
                   System.Data.DataTable table=ds.Tables[0] ;
                   foreach(DataColumn col in table.Columns)
                   {
                          colIndex++;   
                          excel.Cells[1,colIndex]=col.ColumnName;               
                   }
 
                   foreach(DataRow row in table.Rows)
                   {
                          rowIndex++;
                          colIndex=0;
                          foreach(DataColumn col in table.Columns)
                          {
                                 colIndex++;
                                 excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
                          }
                   }
                   excel.Visible=false;   
            excel.ActiveWorkbook.SaveAs(strExcelFileName+".XLS",Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null,null);
                   excel.Quit();
                   excel=null;
                   GC.Collect();//垃圾回收
}
 
        }   
4、 利用xml实现
Xml:数据的集合 xsl:数据的显示样式
缺点:客户端需要NetFramework
优点:格式比较灵活,用于数据量小的情况
1--Xml
<?xml version="1.0" encoding="utf-8" ?>
<Students>
<Student ID="1">
        <Name>John Doe</Name>
        <Age>18</Age>
        <GPA>3.95</GPA>
        <LockerCombination>10-12-35</LockerCombination>
        <Class Title="Biology" Room="100" />
        <Class Title="English Lit" Room="101" />
</Student>
</Students>
1--Xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 
<xsl:template match="/">
 <html>
 <head>
    <title>Students Listing</title>
 </head>
 <body>
    <table width="750" border="0" cellspacing="0" cellpadding="3" style="border:1px; border-style:solid; border-color:#000000;">
      <tr bgcolor="#E6E6FA">
        <td align="middle" colspan="3">
          <font size="2"><b>Student Listing</b></font>
        </td>
      </tr>
      <tr bgcolor="#E6E6FA">
        <td>
          <font size="2"><b>Student</b></font>
        </td>
        <td>
          <font size="2"><b>GPA</b></font>
        </td>
        <td>
          <font size="2"><b>Age</b></font>
        </td>
      </tr>
 
      <xsl:for-each select="//Student">
        <tr>
           <td>
            <font size="2">
              <b><xsl:value-of select="Name" /></b>
    </font>
          </td>
          <td>
            <font size="2">
              <b><xsl:value-of select="GPA" /></b>
            </font>
          </td>
          <td>
    <font size="2">
              <b><xsl:value-of select="Age" /></b>
    </font>
          </td>
        </tr>
          <tr bgcolor="#eff7de">
            <td colspan="3">
      <table width="100%" border="0" style="border:1px; border-style:solid; border-color:#c0c0c0;" cellspacing="0" cellpadding="3">
              <xsl:for-each select="Class">
        <tr>
                  <td width="75%">
            <font size="2"><xsl:value-of select="@Title" /></font>
                  </td>
                  <td align="right" width="25%">
            <font size="2">Room <xsl:value-of select="@Room" /></font>
                  </td>
                </tr>
              </xsl:for-each>
      </table>
            </td>
          </tr>
      </xsl:for-each>
 
    </table>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>
 
private void Page_Load(object sender, System.EventArgs e)
        {
               XslTransform xslt = new XslTransform();
               xslt.Load(Server.MapPath( "StudentsToHTML.xsl") );
 
               XPathDocument XDoc = new XPathDocument(Server.MapPath( "Students.XML" ));
               XmlWriter writer = new XmlTextWriter( Server.MapPath("Students.html"), System.Text.Encoding.UTF8 );
               xslt.Transform( XDoc, null, writer );
               writer.Close();
               Response.Redirect("Students.html");
 
        }
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值