Overview
◆ The B/S structure causes Web ApplicationsProgram.
◆ The program runs in the browser and the printer runs locally, but files may run on the server, resulting in inflexible print control.
◆ How to control and customize the format is a problem we may face during development.
Document generation
1. client script
Generally, JavaScript can be used to analyze the content of the Source Page and extract the elements of the page to be printed for printing. By analyzing the content of the source document, you can generate a printed target document.
Advantage: the client generates the target document independently to reduce the server load;
Disadvantages: The analysis operations of the source document are complex, and the printed content in the source document must be agreed.
2. server-side program
Use backgroundCodeRead the print source from the database and generate the printed target document. When the page is generated, you should also consider using CSS to implement forced paging control.
Advantage: You can generate a rich set of printed target documents, which are highly controllable. Since the printed content is obtained from the database, the generation operation is relatively simple;
Disadvantage: the server load is relatively large.
Page settings
◆ Page settings mainly refer to setting the margins, headers, footers, and paper of printed documents. Page settings directly affect the generation of the printed document layout, so it is closely related to the generation of printed documents. For example, the number of rows, size, position, and font size of a table.
The existing technology is to use the built-in print template of IE 6.0 to control page settings, which can have a great impact on printing target documents. The print template can control the page margins, headers, footers, and even pages, get user settings, and send settings to the server. The print template technology allows you to customize the preview window and print format to maximize the impact on the target document and print effect.
Print ie directly
Directly call the execwb method of the window. Print or webrower control to print.
Advantage: it is convenient and convenient, and the client does not need any settings.
Disadvantage: The printing control is not flexible. If you call it directly.
Window. Print to print the page. Other elements on the page will also be printed, and the format at the end of the page is not easy to control.
Common method: In most cases, the query result is bound to the DataGrid and the DataGrid is printed. In this case, the printing format is generally relatively fixed and simple, and will not be changed after confirmation. Therefore, ie can be used for direct printing.
[Instance Code]
Note: ① The client prints the specified content through window. Print. Sprnstr and eprnstr are defined here to specify the content.
Run the Code:
<Input type = "button" name = "print" value = "preview and print" onclick = "Preview ()"> |
② If you use window. Print directly, all the content on the page will be printed, but we can use:
St <style> @ Media Print {. noprn {display: None }}
|
Specifies the content that is not printed.
Script Language = "JavaScript">
Function Preview ()
{
Bdhtml###doc ument. Body. innerhtml;
Sprnstr = "<! -- Startprint --> ";
Eprnstr = "<! -- Endprint --> ";
Prnhtml = bdhtml. substr (bdhtml. indexof (sprnstr) + 17 );
Prnhtml = prnhtml. substring (0, prnhtml. indexof (eprnstr ));
Too many Doc ument. Body. innerhtml = prnhtml;
Window. Print ();
}
</SCRIPT>
<! -- Omitting some code -->
<Form ID = "webform1" method = "Post" runat = "server">
<Center> This part is not printed. </center>
<! -- Startprint -->
<Div align = "center">
<Asp: DataGrid id = "dgshow" runat = "server">
<! -- Omitting some code -->
</ASP: DataGrid>
</Div>
<! -- Endprint -->
<Center> This part is not printed below </center>
<Div align = "center">
<Input type = "button" name = "print" value = "preview and print" onclick = "Preview ()">
</Div>
<Style> @ Media Print {. noprn {display: None }}
</Style>
<P class = "noprn"> do not print </P>
<Table id = "DataGrid">
<Tr>
<TD> Print </TD>
</Tr>
</Table>
<Input class = "noprn" type = "button" onclick = "window. Print ()" value = "print">
</Form> |
Webbrowser Control Technology
◆ Print operation implementation
This function is mainly implemented by using the webbrowser control function interface to print, print preview (default), page settings (default ).
<Object ID = 'webbrowser1' width = 0 Height = 0
Classid = 'clsid: 8856f961-340a-11d0-a96b-00c04fd705a2 '>
// Print
Webbrowser1.execwb (6, 1 );
// Print settings
Webbrowser1.execwb (8, 1 );
// Print and preview
Webbrowser1.execwb (7,1 );
// Print directly
Webbrowser1.execwb (6, 6 );
|
When converting a DataGrid to the corresponding HTML code, if a button Column exists, an error is reported. It is best to hide this column. Generally, only data columns can be converted. Second, pay attention to the paging problem. Generally, only one page can be printed. It is best to remove the page before printing. Export to excel and print in word. It can be performed on the server or client.
Advantage: This method is highly adaptable and controllable.
Disadvantage: for use on the server, you must install Word and Excel on the server. for use on the client, you must have certain requirements on the security settings of IE on the client.
[Instance Code]
Protected void btnmime_click (Object sender, system. eventargs E)
{
Binddata ();
Response. contenttype = "application/vnd. MS-excel ";
Response. addheader ("content-disposition", "inline; Filename = "+ httputility. urlencode (" download file .xls ", encoding. utf8 ));
// If the output is word, modify it to the following code:
// Response. contenttype = "application/MS-word"
// Response. addheader ("content-disposition", "inline?filename=test.doc ")
Stringbuilder sb = new stringbuilder ();
System. Io. stringwriter Sw = new system. Io. stringwriter (SB );
System. Web. UI. htmltextwriter hW = new system. Web. UI. htmltextwriter (SW );
SB. append ("");
Dgshow. rendercontrol (HW );
SB. append ("");
Response. Write (sb. tostring ());
Response. End ();
}
Protected void btncom_click (Object sender, system. eventargs E)
{
Exporttoexcel (binddata (), server. mappath ("comexcel.xls "));
}
// From dataset to excel
# Region from dataset to excel
/// Export the specified Excel File
Public void exporttoexcel (Dataset ds, string strexcelfilename)
{
If (Ds. Tables. Count = 0 | strexcelfilename = "") return;
Doexport (DS, strexcelfilename );
}
/// Execute Export
Private void doexport (Dataset ds, string strexcelfilename)
{
Excel. Application Excel = new excel. Application ();
Int rowindex = 1;
Int colindex = 0;
Excel. application. workbooks. Add (true );
System. Data. datatable table = Ds. Tables [0];
Foreach (datacolumn Col in table. columns)
{
Colindex ++;
Excel. cells [1, colindex] = col. columnname;
}
Foreach (datarow row in table. Rows)
{
Rowindex ++;
Colindex = 0;
Foreach (datacolumn Col in table. columns)
{
Colindex ++;
Excel. cells [rowindex, colindex] = row [col. columnname]. tostring ();
}
}
Excel. Visible = false;
Excel. activeworkbook. saveas (strexcelfilename + ". xls ", Excel. xlfileformat. xlexcel9795, null, null, false, false,
Excel. xlsaveasaccessmode. xlnochange, null, null );
Excel. Quit ();
Excel = NULL;
GC. Collect (); // garbage collection
}
# Endregion |
Use the. NET component to print
Using. NET components
◆ Advantages: This printing method is suitable for applications with large format changes and small data volumes.
◆ Disadvantages:
◆ Client Security. NET Framework components are required.
◆ XML parsing is not ideal if the file speed is large.
◆ There will be significant latency when the page is loaded for the first time.
Use XSL and XSLT to convert XML
◆ XSL: Extended style sheet language, which can be used to convert XML into other text formats.
◆ XSL conversion includes discovering or selecting a pattern match. You can use XPath to select a result set and define the result output for each item in the result set.
◆ XSL is a powerful tool that converts XML into any format you want.
[Reference Code]
Transform transform XSLT = new transform ();
XSLT. Load (server. mappath ("studentstohtml. XSL "));
Xpathdocument xdoc = new xpathdocument (server. mappath ("students. xml "));
Xmlwriter writer = new xmltextwriter (server. mappath ("students.html "), System. Text. encoding. utf8 );
XSLT. Transform (xdoc, null, writer );
Writer. Close ();
Response. Redirect ("students.html ");
|
Print with ActiveX Control
Use third-party controls
◆ Develop controls by yourself. In this way, many commercial software use this method. After writing controls, it is no longer necessary to use them on the web or in applications.
Advantage: The printing method is very flexible, and basically the web that the program can achieve can also be done.
Disadvantage: the client needs to install components, which is not very convenient to deploy.
Use Crystal Reports
◆ You only need a web browser to view reports.
◆ The report viewer control can be one of the many controls in the application.
◆ Easy interaction with reports
◆ You can export a report in the Microsoft Word and Excel formats, PDF, HTML, and Crystal Reports for Visual Studio. NET formats.
◆ You can use the report control to print it directly.
[Instance Code]
// Fill in the Crystal Report and omit the connection code
Myreport reportdoc = new myreport ();
Reportdoc. setdatasource (DS );
CRV. reportsource = reportdoc;
// The output is a file of the specified type.
Crystaldecisions. Shared. diskfiledestinationoptions diskopts = new Crystaldecisions. Shared. diskfiledestinationoptions ();
Reportdoc. exportoptions. exportdestinationtype = Crystaldecisions. Shared. exportdestinationtype. diskfile;
String strfilename = server. mappath ("output ");
Switch (ddlformat. selecteditem. Text)
{
Case "Rich Text (RTF )":
Reportdoc. exportoptions. exportformattype =
Crystaldecisions. Shared. exportformattype. richtext;
Diskopts. diskfilename = strfilename + ". rtf ";
Break;
Case "Portable Document (PDF )":
Reportdoc. exportoptions. exportformattype = Crystaldecisions. Shared. exportformattype. portabledocformat;
Diskopts. diskfilename = strfilename + ". pdf ";
Break;
Case "MS Word (DOC )":
Reportdoc. exportoptions. exportformattype = crystaldecisions. shared. exportformattype. wordforwindows; diskopts. diskfilename = strfilename + ". doc "; break; case" MS Excel (xls) ": reportdoc. exportoptions. exportformattype =
crystaldecisions. shared. exportformattype. excel; // diskopts. diskfilename = strfilename + ". xls "; break; default: break; }< br> reportdoc. exportoptions. destinationoptions = diskopts; reportdoc. export (); // print // specify the printer name string strprintername; strprintername = @ "Canon Bubble-jet BJC-210SP"; // set the print margins Pagemargins margins; margins = reportdoc. printoptions. pagemargins; margins. bottommargin = 250; margins. leftmargin = 350; margins. rightmargin = 350; margins. topmargin = 450; reportdoc. printoptions. applypagemargins (margins); // name of the application printer reportdoc. printoptions. printername = strprintername; // print the report. Set startpagen and endpagen // to 0 to print all pages. reportdoc. printtoprinter (1, false, 0, 0);
|