During front-end development, the function of importing data to excel through js is often used. Now a simple demo code is provided for future use!
[Javascript]
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 specific operation: \ 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 specific operation: \ 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.2cto.com
} Else {
Alert ("Please use IE browser to" import to EXCEL "operation! "); // You can easily set the security level to IE.
}
}
}
Author: love _ coder