Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<TITLE> export an Excel file </TITLE>
<Script type = "text/javascript">
Var idTmr = "";
Function killExcelProcess (appExcel _){
AppExcel _. Quit ();
AppExcel _ = null;
IdTmr = window. setInterval ("Cleanup ();", 1 );
}
// Export to excel. The tableid parameter is the id of the table to be exported on the JSP page. You need to change the security settings of IE. ActiveX is enabled. If not, run "regsvr32 scrrun. dll"
Function exportToExcel (tableid, notitleandsearch ){
Try {
ClipboardData. setData ('text ','');
Var appExcel = new ActiveXObject ("Excel. Application ");
KillExcelProcess (appExcel );
AppExcel. workbooks. add;
// Title
If (notitleandsearch = null | notitleandsearch = false ){
Var elTable = document. getElementById ('div _ title ');
Var oRangeRef = document. body. createTextRange ();
ORangeRef. moveToElementText (elTable );
ORangeRef.exe cCommand ("Copy ");
AppExcel. ActiveSheet. Cells (1, 3). select ();
AppExcel. ActiveSheet. Paste ();
ClipboardData. setData ('text ','');
AppExcel. ActiveSheet. Cells (2, 1). select ();
AppExcel. ActiveSheet. Paste ();
}
Var elTable1 = document. getElementById (tableid );
Var oRangeRef1 = document. body. createTextRange ();
ORangeRef1.moveToElementText (elTable1 );
ORangeRef1.execCommand ("Copy ");
AppExcel. WorkSheets (1). Activate;
If (notitleandsearch = null | notitleandsearch = false ){
AppExcel. ActiveSheet. Cells (3, 1). select ();
} Else {
AppExcel. ActiveSheet. Cells (1, 1). select ();
}
AppExcel. WorkSheets (1). Activate;
AppExcel. ActiveSheet. Paste ();
AppExcel. Visible = true;
} Catch (e ){
Alert ("Please confirm IE Security Settings, ActiveX enabled! ");
Return false;
}
ClipboardData. setData ('text ','');
}
</Script>
</HEAD>
<BODY>
<Button onclick = "javascript: exportToExcel ('testlist',''); "> export </button> <br>
<Div id = 'div _ title'>
<Font color = 'black' size = '4'> <strong> export a Report </strong> </font>
</Div>
<Table id = "testList" bordercolor = "#000000" border = "1">
<Tr>
<Td> sequence </td>
<Td> name </td>
<Td> quantity </td>
</Tr>
<Tr>
<Td> 1 </td>
<Td> Michael Jacob 1 </td>
<Td> 2 </td>
</Tr>
<Tr>
<Td> 2 </td>
<Td> JOHN 3 2 </td>
<Td> 2 </td>
</Tr>
<Tr>
<Td> 3 </td>
<Td> James 3 </td>
<Td> 2 </td>
</Tr>
<Tr>
<Td> 4 </td>
<Td> James 4 </td>
<Td> 2 </td>
</Tr>
<Tr>
<Td> 5 </td>
<Td> zhangsan 5 </td>
<Td> 2 </td>
</Tr>
<Tr>
<Td colspan = "2"> total: </td>
<Td> 10 </td>
</Tr>
</Table>
</BODY>
</HTML>