JavaScript将页面中的table数据导出到excel

  js.

 

function toexcel() { // start excel and get application object. var oxl = new ActiveXObject("excel.application"); // get a new workbook. var owb = oxl.workbooks.add(); var osheet = owb.activesheet; var table = document.all.GridView1; var hang = table.rows.length; var lie = table.rows(0).cells.length; // add table headers going cell by cell. for (i = 0; i < hang; i ++ ) { for (j = 0; j < lie; j ++ ) { osheet.cells(i + 1, j + 1).value = table.rows(i).cells(j).innerText; } } oxl.visible = true; oxl.usercontrol = true; } 

 

Html.

<input type="button" name="button2" value="导出EXCEL" onclick="toexcel();" /> <table cellspacing="0" cellpadding="4" border="0" id="GridView1" style="color:#333333;border-collapse:collapse;" mce_style="color:#333333;border-collapse:collapse;"> <tr style="color:White;background-color:#507CD1;font-weight:bold;" mce_style="color:White;background-color:#507CD1;font-weight:bold;"> <th scope="col">姓名</th><th scope="col">性别</th><th scope="col">年龄</th><th scope="col">电话1</th> </tr><tr style="background-color:#EFF3FB;" mce_style="background-color:#EFF3FB;"> <td>张三0</td><td>男</td><td>0</td><td>0755-29102930</td> </tr><tr style="background-color:White;" mce_style="background-color:White;"> <td>张三1</td><td>男</td><td>1</td><td>0755-29102931</td> </tr><tr style="background-color:#EFF3FB;" mce_style="background-color:#EFF3FB;"> <td>张三2</td><td>男</td><td>2</td><td>0755-29102932</td> </tr><tr style="background-color:White;" mce_style="background-color:White;"> <td>张三3</td><td>男</td><td>3</td><td>0755-29102933</td> </tr><tr style="background-color:#EFF3FB;" mce_style="background-color:#EFF3FB;"> <td>张三4</td><td>男</td><td>4</td><td>0755-29102934</td> </tr><tr style="background-color:White;" mce_style="background-color:White;"> <td>张三5</td><td>男</td><td>5</td><td>0755-29102935</td> </tr> </table> 

你可能感兴趣的:(JavaScript将页面中的table数据导出到excel)