經典ASP.NET列印技術

來源:互聯網
上載者:User

對於B/S架構的項目中,Web列印最是讓程式員頭痛的技術,在這次項目中運用了第三方控制項ScriptX解決了此技術包括各種匯總表,詳細清單等等,但最大弊端就是列印的格式是死的,你要列印出怎樣的格式必須自己來做,還有就是列印分頁,它是按你設定的紙張大小來分的,所以必須人為控制,ScriptX控制項所需檔案ScriptX.cab。(ScriptXhttp://www.meadroid.com/scriptx/freedep.asp )
列印功能使用:
 
⑴ 使用者在用戶端單擊“列印”按鈕,第一次使用列印會彈出如下對話方塊
 
⑵ 單擊“是”按鈕,此時會把控制項ScriptX下載到本機
 
⑶ 在列印視窗中有大標題、列印條件、列印列表,單擊“列印”按鈕進入列印瀏覽介面。
 
⑷ 單擊“Print”按鈕,聽印表機聲音響起。
二、    一個簡單的ScriptX控制項使用執行個體
1.介面檔案  WebExample.aspx  (其中kg.css是介面樣式在這裡就不介紹了)

以下為引用的內容:
〈%@ Page language="c#" Codebehind="WebExample.aspx.cs" AutoEventWireup="false" Inherits="WebPrint.WebExample" %>
〈!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
〈HTML>
       〈HEAD>
              〈title>一個簡單的ScriptX列印〈/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">
              〈LINK href="kg.css" type="text/css" rel="stylesheet">
       〈/HEAD>
       〈body MS_POSITIONING="GridLayout">
              〈form id="WebExample" method="post" runat="server">
                     〈TABLE width="100%" height="100%">
                            〈tr>
                                   〈td height="100">
                                   〈/td>
                            〈/tr>
                            〈tr>
                                   〈td style="HEIGHT: 14px" align="center">
                                          〈div id="divTableResult" name="divTableResult">
                                                 〈asp:Label id="Label1" runat="server" Width="232px" Height="32px" Font-Size="Large">歡迎使用ScriptX列印〈/asp:Label>
                                          〈/div>
                                   〈/td>
                            〈/tr>
                            〈tr>
                                   〈td align="center" valign="top">
                                          〈div id="divChartResult" name="divChartResult">
                                                 〈asp:Image id="Image1" runat="server" ImageUrl="win2000.gif">〈/asp:Image>
                                          〈/div>
                                   〈/td>
                            〈/tr>
                            〈tr>
                                   〈td align="center" valign="top">
                                          〈input id="btnPrint" style="WIDTH: 32px; HEIGHT: 19px" onclick="printResult();" type="button"
                                                 size="1" value="列印" name="btnPrint">
                                   〈/td>
                            〈/tr>
                     〈/TABLE>
              〈/form>
              〈script language="javascript">
              function printResult()
              {
                     //針對不同模組,設定列印參數
                     var title = 'ScriptX列印';//報表名稱
                     var orien = 'true';//列印頁面方向,true:縱向列印/false橫向列印
                     //參數設定結束
                     var strURL;
                     title = escape(title);
                     //condition = escape(condition);
                     strURL = 'printContainer2.aspx?title='+title+'&orien='+orien;
                     window.open(strURL,'printResult','height=600, width=800, top=0, left=50,toolbar=no , menubar=no, scrollbars=yes, resizable=no, location=no, status=no'); 
              }
              〈/script>
       〈/body>
〈/HTML>

說明:

以下為引用的內容:
⑴ 〈div id="divTableResult" name="divTableResult">和〈div id="divChartResult" name="divChartResult"> 是在printContainer2.aspx檔案中定義的傳值標籤,因此必須要有。
⑵ printResult()方法用來聲明傳值內容,具體在方法中已說明

 

2.列印介面   printContainer2.aspx (其中print.css是列印樣式就不介紹了)
〈%@ Page language="c#" Codebehind="printContainer2.aspx.cs" AutoEventWireup="false" Inherits="WebPrint.printContainer2" %>
〈!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
〈%
string title = "查詢列印";
‘用於title擷取標題
if(Request.QueryString["title"].ToString().Trim()!=""){
       title = Request.QueryString["title"].ToString().Trim();
}
‘用於orientation擷取列印頁面方向,true:縱向列印/false橫向列印
string orientation = "true";
if(Request.QueryString["orien"].ToString().Trim()!=""){
       orientation =  Request.QueryString["orien"].ToString().Trim();
}
%>

以下為引用的內容:
〈html>
       〈head>
              〈title>ScriptX 列印〈/title>
              〈meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
              〈meta name="CODE_LANGUAGE" Content="C#">
              〈meta name="vs_defaultClientScript" content="JavaScript">
              〈meta http-equiv="content-type" content="text/html; charset=gb2312">
              〈meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
              〈link href="kg.css" rel="stylesheet" type="text/css">
              〈link href="print.css" rel="stylesheet" type="text/css"  media="print" >
〈script  FOR=document event="onclick">
if(event.srcElement.tagName=='A'){
              //alert('a click!');
              event.cancelBubble = true;
}

 

〈/script>
              
〈style type="text/css">
a:link {text-decoration:none}
a:visited {text-decoration:none}
a:active {text-decoration:none}
a:hover {text-decoration:none}

A { COLOR: #000000;TEXT-DECORATION: none; FONT-SIZE: 8pt;FONT-FAMILY: "";}

〈/style>
       〈/head>
       
       〈body MS_POSITIONING="GridLayout" >
〈!--這裡調用控制項ScriptX.cab-->
〈object id="factory" viewastext  style="display:none"
  classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
  codebase="ScriptX.cab#Version=6,1,431,2">
〈/object>
〈SCRIPT defer>
//用於設定列印參數
function printBase() {
factory.printing.footer = "&b ScriptX 列印 &b"       //頁首
factory.printing.footer = ""                                 //頁尾
factory.printing.portrait = 〈%=orientation%>             //true為縱向列印,flase為橫向列印
factory.printing.leftMargin = 1.5                               //左頁面邊界
factory.printing.topMargin = 0.5                               //上頁面邊界
factory.printing.rightMargin = 0.5                             //右頁面邊界
factory.printing.bottomMargin = 1.0                          //下頁面邊界
}
〈/SCRIPT>
〈table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" bordercolor="#FFFFFF">
  〈tr bordercolor="#FFFFFF">
    〈td >
       〈table width="70%" border="0" cellspacing="0" cellpadding="2" align="center">
         〈tr>
           〈td align="center">〈font size="5" face="宋體">〈b>〈div  name="divTitle" id="divTitle">〈/div>〈/b>〈/font>〈/td>
         〈/tr>
         〈tr>
           〈td align="center">
             〈div id="divReport" name="divReport">
             〈input type="button" name="cmdPRINT" id="cmdPRINT" onclick="printReport()" value="列印">
                   
             〈input type="button" onclick="window.close();"  value="關閉">
             〈/div>
           〈/td>
         〈/tr>
       〈/table>
   〈/td>
  〈/tr>
〈/table>
〈table width="100%" border="0" cellspacing="0" cellpadding="2" align="center">
         〈tr>〈td align="center">
〈!--  用於顯示divTableContainer標籤的值 -->
〈div id="divTableContainer"  name="divTableContainer">〈/div>
              〈/td>〈/tr>
               〈tr>〈td align="center">
〈!--  用於顯示divChartContainer標籤的值 -->
〈div id="divChartContainer"  name="divChartContainer">〈/div>
              〈/td>〈/tr>
〈/table>
〈/body>
〈script language="javascript">
//使介面最大化
maxWin();

var needcon='';
document.all.divTitle.innerText = unescape('〈%=title%>');

//用於把父表單的divTableResult和divChartResult標籤的值分別轉入divTableContainer 和divChartContainer標籤中
var tableContent = window.opener.document.all.divTableResult.innerHTML;
tableContent = replaceAll(tableContent,"〈A ","〈span ");
tableContent = replaceAll(tableContent,"〈/A> ","〈/span> ");
tableContent = replaceAll(tableContent,"〈a ","〈span ");
tableContent = replaceAll(tableContent,"〈/a> ","〈/span> ");
tableContent = replaceAll(tableContent,"〈input ","〈font ");
tableContent = replaceAll(tableContent,"〈INPUT ","〈font ");
document.all.divTableContainer.innerHTML = tableContent;
document.all.divChartContainer.innerHTML = window.opener.document.all.divChartResult.innerHTML;

//用於調用設定列印參數的方法和顯示預覽介面
function printReport(){
       printBase();
       //window.print();
       factory.printing.Preview();

function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );
    while ( idx > -1 ) {
        str = str.replace( from, to ); 
        idx = str.indexOf( from );
    }
    return str;
}
function maxWin()
{
     var aw = screen.availWidth;
     var ah = screen.availHeight;
     window.moveTo(0, 0);
     window.resizeTo(aw, ah);
}
〈/script>
〈/html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.