Simple Example of Js exporting table content to Excel

Source: Internet
Author: User

Copy codeThe Code is as follows:
Function ExportToExcel (tableId) // read each unit in the table to EXCEL
{
Try {
Var curTbl = document. getElementById (tableId );
Var oXL = new ActiveXObject ("Excel. Application ");
// Create an AX object excel
Var oWB = oXL. Workbooks. Add ();
// Obtain the workbook object
Var oSheet = oWB. ActiveSheet;

Var lenRow = curTbl. rows. length;
// Obtain the number of rows in the table
For (I = 0; I <lenRow; I ++)
{
Var lenCol = curTbl. rows (I). cells. length;
// Obtain the number of columns in each row
For (j = 0; j <lenCol; j ++)
{
OSheet. Cells (I + 1, j + 1). value = curTbl. rows (I). cells (j). innerText;

}
}
OXL. Visible = true;
// Set the excel visible attribute
} Catch (e ){
If ((! + '/V1') {// IE browser
Alert ("Excel cannot be started. Please make sure that Excel is installed on your computer! /N if Excel is already installed, "+" adjust the security level of IE. /N: /n "+" Tools → Internet Options → Security → Custom Level → ActiveX controls and ins → initialize and execute scripts for ActiveX controls not marked as secure scripts → enable → OK ");
} Else {
Alert ("Please use IE browser to" import to EXCEL "operation! "); // You can easily set the security level to IE.
}
}
}

Function ExportToExcel (tableId) // read each unit in the table to EXCEL
{
Try {
Var curTbl = document. getElementById (tableId );
Var oXL = new ActiveXObject ("Excel. Application ");
// Create an AX object excel
Var oWB = oXL. Workbooks. Add ();
// Obtain the workbook object
Var oSheet = oWB. ActiveSheet;

Var lenRow = curTbl. rows. length;
// Obtain the number of rows in the table
For (I = 0; I <lenRow; I ++)
{
Var lenCol = curTbl. rows (I). cells. length;
// Obtain the number of columns in each row
For (j = 0; j <lenCol; j ++)
{
OSheet. Cells (I + 1, j + 1). value = curTbl. rows (I). cells (j). innerText;

}
}
OXL. Visible = true;
// Set the excel visible attribute
} Catch (e ){
If ((! + '/V1') {// IE browser
Alert ("Excel cannot be started. Please make sure that Excel is installed on your computer! /N if Excel is already installed, "+" adjust the security level of IE. /N: /n "+" Tools → Internet Options → Security → Custom Level → ActiveX controls and ins → initialize and execute scripts for ActiveX controls not marked as secure scripts → enable → OK "); www.th7.cn
} Else {
Alert ("Please use IE browser to" import to EXCEL "operation! "); // You can easily set the security level to IE.
}
}
}

Related Article

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.