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"