需要引用以下几个主要文件
<script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script> <script type="text/javascript" src="jszip.min.js"></script> <script type="text/javascript" src="external/FileSaver.js"></script> <script type="text/javascript" src="scripts/excel-gen.js"></script> <script type="text/javascript" src="scripts/demo.page.js"></script>
html表格数据,表格id为test_table
<table class="table table-bordered table-striped" id="test_table"> <thead> <tr> <td>等级</td> <td>姓名</td> <td>城市</td> <td>州</td> <td>部门</td> <td>选取</td> <td>票数</td> </tr> </thead> <tbody> <tr> <td> <span>1</span> </td> <td> <span>Small, Steve </span> </td> <td> <span>New York</span> </td> <td> <span>NY</span> </td> <td> <span>Eastern</span> </td> <td> <span>Metropolitan Region</span> </td> <td> <span>378</span> </td> </tr> <tr> <td> <span>2</span> </td> <td> <span>Novello, Rodolfo </span> </td> <td> <span>Lawrence</span> </td> <td> <span>NY</span> </td> <td> <span>Eastern</span> </td> <td> <span>Long Island Region</span> </td> <td> <span>223</span> </td> </tr> </tbody> </table>
js代码
$(document).ready(function () { excel = new ExcelGen({ "src_id": "test_table", "show_header": true }); $("#generate-excel").click(function () { excel.generate(); }); });
用正则先对表格数据过滤掉样式代码,在导出execel.
例子:
<td style="background:red;color:#fff;border:1px solid red;">内容</td>
js:
var reg = /style\s*?=\s*?(['"])[\s\S]*?\1/; var result = '<td style="background:red;color:#fff;border:1px solid red;">内容</td>'.replace( reg , '');
结果:
<td >内容</td>