Js Export table content to excel simple example _javascript tips

Source: Internet
Author: User

Copy Code code as follows:

function exportToExcel (tableId)/read each cell 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 ();
Get Workbook Object
var osheet = Owb.activesheet;

var lenrow = curTbl.rows.length;
Get the number of rows in the table
for (i = 0; i < Lenrow; i++)
{
var lencol = curtbl.rows (i). Cells.length;
Get the number of columns per row
for (j = 0; J < Lencol; J + +)
{
Osheet.cells (i + 1, j + 1). Value = Curtbl.rows (i). Cells (j). innertext;

}
}
oXL.Visible = true;
Set Excel Visible Properties
}catch (e) {
if ((!+ '/v1 ')) {//ie browser
Alert ("Unable to start Excel, make sure you have excel!/n/n installed on your computer if you have Excel installed," + "adjust the security level of IE.) /n/n specific operations:/n/n "+" tools →internet options → security → custom level →activex controls and Plug-ins → initialize and execute scripts → enable → OK for ActiveX controls not marked for safe execution;
}else{
Alert ("Import to Excel" using IE browser!)  "); Easy to set security level, limited to IE browser
}
}
}

function exportToExcel (tableId)/read each cell 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 ();
Get Workbook Object
var osheet = Owb.activesheet;

var lenrow = curTbl.rows.length;
Get the number of rows in the table
for (i = 0; i < Lenrow; i++)
{
var lencol = curtbl.rows (i). Cells.length;
Get the number of columns per row
for (j = 0; J < Lencol; J + +)
{
Osheet.cells (i + 1, j + 1). Value = Curtbl.rows (i). Cells (j). innertext;

}
}
oXL.Visible = true;
Set Excel Visible Properties
}catch (e) {
if ((!+ '/v1 ')) {//ie browser
Alert ("Unable to start Excel, make sure you have excel!/n/n installed on your computer if you have Excel installed," + "adjust the security level of IE.)  /n/n specific operations:/n/n "+" tools →internet options → security → custom level →activex controls and Plug-ins → initialize and execute scripts → enable → OK for ActiveX controls not marked for safe execution; www.th7.cn
}else{
Alert ("Import to Excel" using IE browser!)  "); Easy to set security level, limited to IE browser
}
}
}

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.