Two types of web pages can be printed at the same time (download Excel and print the pages directly)

Source: Internet
Author: User
1. Download the Excel file (refer to the favorites folder)

1 Public   Void Printtable (system. Web. UI. webcontrols. Table table)
2 {
3 System. Io. stringwriter SW = New System. Io. stringwriter ();
4 Foreach (Tablerow R In Table. Rows)
5 {
6 Foreach (Tablecell C In R. cells)
7 {
8Sw. Write (C. Text+"\ T");
9}
10 Sw. Write ( " \ N " );
11 }
12 Sw. Close ();
13 Response. addheader ( " Content-Disposition " , " Attachment; filename=table.xls " );
14 Response. contenttype =   " Application/MS-Excel " ;
15 Response. contentencoding = System. Text. encoding. getencoding ( " Gb2312 " );
16 Response. Write (SW );
17 Response. End ();
18 } Private   Void Btnprint_click ( Object Sender, system. eventargs E)
{
Btnsubmit_click (sender, e );
Printtable (Table1 );
}

Because table output is used at the beginning and stateview cannot be returned, you need to output the table content again (very inefficient) during the submission and printing, and then export the content from the table and output it to the client.

2. Print the page directly

Add HTML on the print pageCode

< Style Media = Print >  
. Noprint {} {Display:None;}  
. Pagenext {} {Page-break-after:Always;}  
</ Style >  

Then add the class attribute to the navigation content.

< Table Cellspacing = "0" Cellpadding = "0" Width = "100%" Border = "0" Class = Noprint >

In this way, only reports can be output without the content in the navigation bar.

Add a webbrowser object on the page

< Object ID = Webbrowser Height = 0 Width = 0 Classid = CLSID: 8856f961-340a-11d0-a96b-00c04fd705a2 Viewastext >   </ Object >

Note that sometimes vs automatically adds object parameters. If an error occurs when using the object page, delete the automatically added parameter values.
Add button

< Input Type = Button Value = Print page Onclick = Document. All. webbrowser. execwb (7,1) >

It is actually the print preview button.

Notes
1. An error occurs when you click "Download excel" and click "print". The client prompts "error: no permission". Why?

2. When using the webbrowser object, the client is required to set "initialize ActiveX controls that are not marked as secure"

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.