How to export a page table to Excel using JavaScript

Source: Internet
Author: User

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>

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.