In some small projects that do not have high requirements, you can use some technologies that are not common and not new, but can actually implement functions well. In this way, the system is not complex to display and can be conveniently maintained.
Create an exportprint.html page with the following code to export to Excel and print webpages.
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> Use JS Technology in IE to export data to Excel and print data </title>
<Style>
. Table_stat {
Border-right: 0px;
Border-bottom: 0px;
Border-left: 1px solid #819BD8;
Border-top: 1px solid #819BD8;
}
. Td_stat {
Border-right: 1px solid #819BD8;
Border-bottom: 1px solid #819BD8;
}
</Style>
</Head>
<Body>
<Object classid = "CLSID: 8856F961-340A-11DO-A96B-00C04FD705A2" height = "0" id = "WebBrowser" width = "0"> </object>
<Table width = "100%" align = "center" border = "0" cellpadding = "0" cellspacing = "0" style = "text-align: center; "class =" table_stat ">
<Tr>
<Td id = "title" align = "center" nowrap = "nowrap" class = "td_stat" colspan = "2">
User Information
</Td>
</Tr>
<Tr>
<Td id = "title" align = "center" nowrap = "nowrap" class = "td_stat" colspan = "1">
Name
</Td>
<Td id = "title" align = "center" nowrap = "nowrap" class = "td_stat" colspan = "1">
Zhang San
</Td>
</Tr>
<Tr>
<Td id = "title" align = "center" nowrap = "nowrap" class = "td_stat" colspan = "2">
<Input type = "button" id = "export" value = "export" onclick = "javascript: exportToExcel ();">
<Input type = "button" id = "print" value = "print" onclick = "javascript: print ();">
</Td>
</Tr>
</Table>
</Body>
</Html>
<Script type = "text/javaScript">
// Export to Excel
Function exportToExcel (){
If (document. getElementById ("title ")){
Try {
Var oRangeRef = document. body. createTextRange ();
ORangeRef.exe cCommand ("Copy ");
Var appExcel = new ActiveXObject ("Excel. Application ");
AppExcel. visible = true;
AppExcel. Workbooks. Add (). WorkSheets. Item (1). Paste ();
} Catch (e ){
Alert ("error! It may be a browser or the data size is too large! ");
Return;
}
AppExcel = null;
ORangeRef = null;
}
}
// Print
Function print (){
If (document. getElementById ("title ")){
Var export = document. getElementById ("export ");
Var print = document. getElementById ("print ");
Try {
Export. style. display = "none ";
Print. style. display = "none ";
Document. all. WebBrowser. ExecWB (6, 1 );
} Catch (e ){
Alert ("error! It may be a browser or the data size is too large! ");
Return;
}
Export. style. display = "";
Print. style. display = "";
}
}
</Script>