JS print and report output 2008-07-15 22:16 one print:
Transferred from: http://hi.baidu.com/afei0211/blog/item/20523955ca8bebc2b745ae98.html
(1) in the actual application! In particular, some companies need report output and print! This time we will use the JS print and report output function! Of course printing is very simple is window.print (); You can also write a return print () in the OnClick event of the button to achieve the printing effect! This time printing you will find the entire page printed out, wrapping the website logo and some other unwanted buttons! This time we can use DIV to control it. Use <div class= "noprint" > Non-printable content </div> Then
<style type= "text/css" media= "print" >
. Noprint{display:none;}
</style>
This time can be achieved when printing the effect of not printing other information!
Description
1. Remove the header and footer: Clear the contents of the header and footer from the browser's page setup options.
2. Print out the background color: Select by using the browser's tools-Internet Options-advanced-print-print background color and image.
(2) You can use WebBrowser printing at the same time. (ie built-in components do not need to install, can achieve print preview and other functions)
Use the method to place the following code in the page: <object id= "WebBrowser" CLASSID=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height= "0" width= "0" >
</object>
Call onclick= "return Userprint ()" in the button
function Userprint ()
{
Document. WEBBROWSER.EXECWB (7,1);//Print Preview
return false;
}
Call WebBrowser for printing operations (parameter list)
WEBBROWSER.EXECWB (*) Open
WEBBROWSER.EXECWB (2,1) Close all IE windows now and open a new window
WEBBROWSER.EXECWB (4,1) Save Web page
WEBBROWSER.EXECWB (6,1) printing
WEBBROWSER.EXECWB (7,1) Print preview
WEBBROWSER.EXECWB (8,1) print Page Setup
WEBBROWSER.EXECWB (10,1) view page Properties
WEBBROWSER.EXECWB (15,1) seems to be withdrawn, pending confirmation.
WEBBROWSER.EXECWB (17,1) Select all
WEBBROWSER.EXECWB (22,1) Refresh
WEBBROWSER.EXECWB (45,1) Close the form without prompting
:
If you want a page to display 2 tables, you can use div to separate 2 tables when you print 2: <div class= "Pagenext" ></div> style: <style media= "Print" Type= "Text/css" >
. Pagenext{page-break-after:always;}
</style>
Two: output to Word or Excel (from the home of gray worm)
Implement ideas, create a Word or Excel object with JavaScript, and then copy the contents of the page that you want to output, and paste it in a Word or Excel object's document.
Output to Word
function Button2_onclick () {
Create a Word Application object
var OWD = new ActiveXObject ("Word.Application");
Add a Document object to the Word application object and remove the range (0) from it
var oRange =owd.documents.add (). Range (0,0);
Create a TextRange object based on an HTML page to navigate to the element you want to print
var sel = Document.body.createTextRange ();
Navigates to the element to be printed in the Textrage object, GridView1 is the ID of the element to be printed
Sel.movetoelementtext (GRIDVIEW1);
Select all HTML content for the GRIDVIEW1 element
Sel.select ();
Copy the selected content to the Clipboard
Sel.execcommand ("Copy");
Paste into the Word document object
Orange.paste ();
Show Word programs and document content
OWD.Application.Visible = true;
Call the print program for Word
OWD.application.printout ();
Close the Word Document object, parameter 0 means that the document is not saved
OWD.application.activedocument.close (0);
Quit Word Program
OWD.application.quit ();
}
Output to Excel
function Button3_onclick () {
var OXL = new ActiveXObject ("Excel.Application");
var owb = OXL.Workbooks.Add ();
var osheet = Owb.activesheet;
var sel=document.body.createtextrange ();
Sel.movetoelementtext (GRIDVIEW1);
Sel.select ();
Sel.execcommand ("Copy");
Osheet.paste ();
oXL.Visible = true;
}
Summary: This way you can quickly convert the content of a page into Word or Excel, and its functionality is implemented on the client side, easing the pressure on the server.
When used here, the browser will prompt "whether to allow access to the Clipboard", which can be set in "Security" in Internet options in IE.
This approach is only suitable for conversions on Windows platforms.
Three. Export the report according to your own Word or Excel file style (from "Gray Bug's Home")
Implementation ideas: Create a Word or Excel document on the server side, bookmark the document, and then write the content to the tag.
(1) Create a Word document that formats the meeting records you want to print and save them to the instance root, named Student.doc.
(2) Insert a bookmark at the specified location in the created Word document. Insert Bookmark First select the text you want to replace, then choose Insert → bookmark option, enter the book signature in the Open dialog box, and click the Add button.
(3) Write a custom JavaScript function that exports the data collected by the form to word and prints it automatically with the following code:
Server-side
protected void Page_Load (object sender, EventArgs e)
{
String str = Request.Url.ToString ();
string s = str. Substring (0, str. LastIndexOf ("/"));
Save the parent path of the current page in hidden
This. Hidden1.value = s + "/";
}
Client
<title> Untitled Page </title>
<script language= "javascript" type= "Text/javascript" >
function Button1_onclick () {
Remove the contents of the hidden
var ss = Document.form1.Hidden1.value;
Create a Word Application object
var WordApp = new ActiveXObject ("Word.Application");
Display the Word application window
Wordapp.visible=true;
To set the path of a Word file template
var s = ss+ "Student.doc";
Open the template you want to fill out
var WordDoc = Wordapp.application.documents.open (s);
var range;
Remove the corresponding bookmark in Word and replace the contents of the page text box.
Range = Wordapp.activedocument.Bookmarks ("Sname"). Range;
Range.Text = Document.form1.txtName.value;
Range = Wordapp.activedocument.bookmarks ("Sage"). Range;
Range.Text = Document.form1.txtAge.value;
Range = Wordapp.activedocument.bookmarks ("Sschool"). Range;
Range.Text = Document.form1.txtSchool.value;
Wordapp.activedocument.printout ();
Close Word Document
Wordapp.activedocument.close (0);
Close the Word application
Wordapp.quit ();
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
Name: <asp:textbox id= "txtname" runat= "Server" ></asp:TextBox>
<br/>
Age: <asp:textbox id= "txtage" runat= "Server" ></asp:TextBox>
<br/>
School: <asp:textbox id= "Txtschool" runat= "Server" ></asp:TextBox>
<br/>
<input id= "Button1" type= "button" value= "Tobookmark" onclick= "return Button1_onclick ()"/><input runat= Server id= "Hidden1" name= "Hidden1" type= "hidden"/></div>
</form>
</body>
Transferred from: http://tiwson.iteye.com/blog/618232
JS print and report output