首页 » 网页前端 » JavaScript » 阅读文章
js 将html中表格导出为excel
标签:JavaScript
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>导出excel</title> <script language="javascript"> function tableToExcel(tname) { if(confirm('是否要导出到excel?')!=0) { window.clipboardData.setData("Text",document.all(tname).outerHTML); try { ExApp = new ActiveXObject("Excel.Application") var ExWBk = ExApp.workbooks.add() var ExWSh = ExWBk.worksheets(1) ExApp.DisplayAlerts = false ExApp.visible = true } catch(e) { alert("导出没有成功!1.您的电脑没有安装Microsoft Excel软件!2.请设置Internet选项自定义级别,对没有标记安全级别的 ActiveX控件进行提示。") return false } ExWBk.worksheets(1).Paste; }else { return; } } </script> </head> <body> <table id="baba"> <tr> <td>afdsfsd</td> <td>fdfsdfds</td> <td>fdfsadfsa</td> <td>fdsfsadfds</td> <td>fdsfasd</td> <td>fdsfsad</td> </tr> <tr> <td>afdsfsd</td> <td>fdfsdfds</td> <td>fdfsadfsa</td> <td>fdsfsadfds</td> <td>fdsfasd</td> <td>fdsfsad</td> </tr> <tr> <td>afdsfsd</td> <td>fdfsdfds</td> <td>fdfsadfsa</td> <td>fdsfsadfds</td> <td>fdsfasd</td> <td>fdsfsad</td> </tr> </table> <input type="button" value="导出" onclick="tableToExcel('baba')" /> </body> </html>
demo:http://download.ezencart.com/demo/export_excel.html
缺点:必须是要求客户端安装了EXCEL,还要设置IE安全选项
声明: 本文由Ezencart原创,转载请保留链接:js 将html中表格导出为excel
评论 共0条 (RSS 2.0) 发表评论