Web Printing Implementation Method

Source: Internet
Author: User

Currently, when developing B/S structured programs, web printing is one of the most difficult and troublesome problems. The printed results are often far from the expected ones. How can we print the desired results? The methods described in this article will solve this problem, making web printing easy to use.

We know there are three ways to implement web printing: the first is to use office tools that everyone is familiar with to implement web printing, such as using word or Excel printing tools to implement web printing, the difficulty of this method is how to import data from a Web page into word or Excel. The second is to use the print control provided by IE browser to implement web printing, the difficulty of this method is how to control the settings of browser printing controls in the program. The third is to use third-party controls or report software to implement web printing, the difficulty of this method is that most of the report software is paid for use, so it has to be considered economically. For the third method, such controls include the FileSystemObject component and scriptx. such as the cab control. This type of report software includes such software as Ruyi report, cell plug-in of yonyou Company, and full-dynamic web reports of beautiful girl. Here we will not describe their usage, as long as you refer to the instructions provided by them to control them. I will focus on the first two methods below.

Use office tools for Printing

The first method is to use the office printing tool for web printing. We mentioned above that the difficulty of this method is to import data into word or Excel. Next I will take excel as an example to introduce how to import data into excel. In fact, there are many ways to import data from webpages into excel. Here we will only introduce one of them, that is, using ActiveX Control -- excel. application. Excel. application is a programming interface provided by Microsoft for excel. In other programming languages, you can use this interface program to Operate Excel tables. The following describes the commonly used commands used to operate Excel tables in a program (using the script language VBScript as an example, similar to other languages ):

1. Set xlapp = Createobject ("Excel. application") 'to create an Excel Object

2. Set xlbook = xlapp. workbooks. Open ("file name") 'open an existing Excel worksheet

3. Set xlbook = xlapp. workbooks. Add () 'create an Excel worksheet

4. xlapp. Visible = true' sets the EXCEL object to be visible (or invisible)

5. Set xlsheet = xlbook. worksheets ("table name") 'sets the activity Worksheet

6. Set xlsheet = xlbook. activesheet 'to set the active Worksheet (Default table name)

7. xlsheet. cells (row, col) = value 'assigns a value to the cell (row, col ).

8. Print the worksheet in xlsheet. printout.

9. xlbook. Close (true) 'Close the workbook

10. xlapp. Quit: end the Excel Object

11. Set xlapp = nothing 'to release the xlapp object

Here is a simple example to illustrate the usage of these commands. The following shows the key implementation code (implemented using JavaScript scripts and VBScript, which can be implemented by interested readers ):

<Script language = "JavaScript">

Function xlprint (){

VaR xlapp; // store Excel objects

VaR xlbook; // store the Excel worksheet File

VaR xlsheet; // store the Excel worksheet

Try {

Xlapp = new activexobject ("Excel. application"); // create an Excel Object}

Catch (e ){

Alert ("enable ActiveX control settings! ");

Return ;}

Xlbook = xlapp. workbooks. Add (); // create an Excel Workbook File

Xlsheet = xlbook. activesheet; // activate an Excel worksheet

VaR rowlen = printdata. Rows. length; // number of rows of the table object

For (VAR I = 0; I <rowlen; I ++ ){

VaR Collen = printdata. Rows (I). cells. length; // Number of columns of the table object

For (var j = 0; j <Collen; j ++) // assign values to cells in an Excel table

Xlsheet. cells (I + 1, J + 1). value = printdata. Rows (I). cells (j). innertext ;}

Xlapp. Visible = true; // set the EXCEL object to be visible}

</SCRIPT>

Code Description: as long as the table ID in the program is set to: printdata, and then the onclick Event Response Function of the print button is set to xlprint; however, to run this program, ie must allow initialization and script running of ActiveX controls that are not marked as secure. The specific setting method is as follows: Open Control Panel → Internet Options → Security → Custom Level → initialize and Run ActiveX controls not marked as secure → select and enable, in this way, our program can run. If this ActiveX Control setting is not enabled, an exception is thrown when the program executes the creation of an Excel object. In this case, you can catch this exception through the catch () Statement and handle it accordingly. If you want to print it directly, you can call the command: xlsheet. printout.

The preceding method imports data from cells on the Web page to cells in Excel. The following describes how to import the entire table into excel. The main idea of this method is to copy the content to be printed on the webpage to the clipboard, and then call the xlsheet. paste () command to copy the content of the clipboard to excel. Let's take a look at its key code:

VaR selectarea = Document. Body. createTextRange (); // create a text area

Selectarea. movetoelementtext (printdata); // focus the text area on printdata

Selectarea. Select (); // select printdata

Selectarea.exe ccommand ("copy"); // copy printdata to the clipboard

Xlsheet. paste (); // copy the data in the clipboard to excel

Using IE browser

In the previous section, we used excel as an example to describe how to use the office printing tool to print web pages. Now let's take a look at how to use the print control provided by IE browser to print web pages. As we all know, Internet Explorer has powerful printing functions. Why don't we use this to implement web printing. The reason is that if we directly use the print function of IE or call window in the program. print () to print the webpage, the webpage title and page number information will appear in the header, and the webpage and date information will appear in the footer. In fact, this information is not what we want to print, so how can we remove this information? The practice is actually very simple. We only need to open the page Setting Dialog Box in the File menu of IE, and then remove the header and footer code (header: & W & B page number, & P/& P footer: & U & B & D). If we print it again, the information will not appear. Now let's take a look at what these codes mean? In fact, these codes are provided by IE to set print pages. The specific meanings of these codes are given below, as shown in the table.

Through the above table, the reader should understand the meaning of the default header and footer in the IE printing settings, and the reader can combine the above Code to set his/her favorite printing settings. Here we are not just talking about this. we imagine that we have developed a system based on the B/S structure, which contains the report section, we cannot allow each client to set the print settings of IE, so we need to control these settings in the program. Below we use Vbscript to write a function to control page settings by modifying the registry key value. The Code is as follows:

<Script language = "VBScript">

Dim path, Reg

'Path stores the Registry address set for IE printing, and Reg stores the objects of the wscript. Shell component.

Path = "HKEY_CURRENT_USER/software/micro-soft/Internet Explorer/pagesetup"

'Modify the print settings through the registry, and only modify the values of the header, footer, and each boundary.

'Parameter Description: Header -- header, footer -- footer, margin_left -- left boundary

'Margin _ top -- upper boundary, margin_right -- right boundary, margin_bottom -- lower boundary

'In the margin settings, 1 corresponds to 25.4mm, that is, margin_left = 1 indicates 25.4mm of the actual value.

Function pagesetup (header, footer, margin_left, margin_top, margin_right, margin_bottom)

On Error resume next

Set Reg = Createobject ("wscript. Shell ")

If err. Number> 0 then

Msgbox "cannot create wscript. Shell object! "

Exit Function

End if

Reg. regwrite path + "/Header", header' sets the header

Reg. regwrite path + "/footer", footer 'sets the footer

Reg. regwrite path + "/margin_left", margin_left 'sets the left boundary.

Reg. regwrite path + "/margin_top", margin_top 'sets the upper boundary.

Reg. regwrite path + "/margin_right", margin_right 'sets the right boundary.

Reg. regwrite path + "/margin_bottom", margin_bottom 'sets the bottom boundary

End Function

</SCRIPT>

In the program, you can write another function (which can be written by VBSCRIPT or Javascript script) to call the function first, and then call window. Print () to print the function. No code is provided here. Careful readers will surely find that the printed page will have a print button on it, and the page is divided by the IE printing control according to the paper size, next we will introduce a method to control the display of pages and print buttons. This method uses CSS style sheets to control these settings, because the style sheet has a "Media = Print" attribute, which takes effect only when being printed, therefore, you can control the printing buttons and pages as follows:

<Style media = print>

. Noprint {display: none ;}

. Pagenext {page-break-after: Always ;}

</Style>

You only need to add the code class = "noprint" to the print button, and add the Code <Div class = "pagenext"> </div> to the page where the page is required. So far, our printing function has been quite complete, but there is no print preview and direct printing function. Generally, users will preview it before printing. If there is no problem, print it directly. The following two functions are implemented through the iewebbrowser component.

First, create the iewebbrowser object:

<Object ID = webbrowser classid = CLSID: 8856f961-340a-11d0-a96b-00c04fd705a2 width = 0 Height = 0> </Object>

The execwb method is called with different parameters to implement print preview and direct printing:

Webbrowser. execwb (6, 1); // print

Webbrowser. execwb (6, 6); // print directly

Webbrowser. execwb (); // print preview

There are many other parameters in the execwb method, which will not be described here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.