Many projects in the middle of the need for the Web page to print output, some print table content, some print local text, at this time can choose a plan of roughly two kinds.
1. Use the component to generate the content of the report Word document or Excel document, export and use Office with the printing function to print
2. Use JavaScript on Web pages or other builds based on JavaScript and other languages
The two methods are described in detail below
1. Generate Word or Excel document, when the content of the area to be printed as text is suitable for export to the Word document processing, when the content to be printed as a table suitable for export as an Excel document, in the JSP can be used Jxl,jacob, POI, such as the establishment of Office documents processing, personal recommendation to use POI, this component is maintained by the Apache Fund Development, its position in the field of open source beyond doubt. The usage of this component is no longer described in detail, and its help documentation has detailed usage methods and sample code. Can meet a variety of basic development needs.
2. There are more ways to use JavaScript or print components, and here are a few scenarios and detailed code for one of the scenarios
A. Use pure JavaScript code:
This method is a simple call window.open () method, the effect of this method is equivalent to the use of IE printing function, will be the entire page to see the content of all printing, and will automatically print out the header and footer.
B. Modifying registry entries, and other solutions for one browser
The principle of this approach is basically to access the registry of the client system, to modify some of the IE options inside, such as masking or modifying a custom print scheme such as a header and footer, this aversion is very obvious, first of all, to modify the registry entries on the Web page requires the client to reduce the security settings of the browser, This poses a significant risk to the customer's computer, and they all find the location of the browser in the registry and then operate, limiting processing to one or several browsers and limiting the client's operating system to Windows
C. Third-party Plug-ins
The use of Third-party plug-ins, such as Scriptx,webprint, this is also the principle of third party is basically the same as the B solution, but also the use of pure JavaScript code for encapsulation, but the use of this plug-in requires the client in the printing time to install Plug-ins, Also need to reduce the client's security settings, if there is a firewall or anti-virus software will not play a role, and the speed of this scheme is very problematic, loading plug-in processing seriously affect the speed of operation, but the printing effect of this scheme is very ideal
D. Compromise
Use WebBrowser and JavaScript and CSS to print the specified area on the page to display the Print Print Preview Page Setup button to set up the print, so that you can remove the header and footer in JavaScript using CSS control hide don't What's printed
The following is a detailed code (note: If you construct a new page with JavaScript, the code is connected, otherwise JavaScript interprets the error):
<script language= "JavaScript" >
function Printme ()
{
var print = This.document.getElementById ("print"). InnerHTML;
Print = print + <br/>
Print = print + ' <script language=javascript> function Printview () {hidden ();d Ocument.all.WebBrowser.ExecWB (7,1) ; function print () {hidden ();d Ocument.all.WebBrowser.ExecWB (6,6)}function PageSetup () {hidden (); Document.all.WebBrowser.ExecWB (8,1);} function hidden () {document.all ("Printview"). style.display= "None"; document.all ("print"). style.display= "None"; document.all ("PageSetup"). style.display= "None";} <//script> ';
Print = print + "<object classid=clsid:8856f961-340a-11d0-a96b-00c04fd705a2 height=21 id=webbrowser width=87> ;</object> <input Id=printview Name=button onclick=printview () Type=button value= print preview > <input id = Print Name=button onclick=print () Type=button value= Direct print > <input id = pageSetup Name=button page Setup () Type=button value= Page setup > ";
var NewWindow = window.open ();
NewWindow.document.open ("text/html");
NewWindow.document.write (print);
NewWindow.document.close ();
}
</script>
<body>
<span id= ' print ' > put the content to be printed here </span>
<p> All content </p>
<a href= "Javascript:printme ()" target= "_self" > Printing </a>
</body>
Code explanation: Put the content to be printed in the ID print area in the Printme function to the output of the content into a new page, in the new page to print processing, in the new page with WebBrowser object for printing processing, with CSS control to hide do not want to print content.