1. 用table2excel 的js文件,这种方法没有IE兼容性
需要在文件中导入
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.4.2.min.js"></script>
<script src="<%=request.getContextPath()%>/js/jquery.table2excel.js"></script>
然后再文件中写方法
function exportexcel(){
$("#tableServer").table2excel({
exclude: ".noExl",
name: "Excel Document Name",
filename: "myFileName",
exclude_img: true,
exclude_links: true,
exclude_inputs: true
});
}
2. js代码
function ExportToExcel() {
var elTable = document.getElementById("tableServer"); //你的tableID
var oRangeRef = document.body.createTextRange();
oRangeRef.moveToElementText(elTable);
oRangeRef.execCommand("Copy");
try {
var appExcel = new ActiveXObject("Excel.Application");
} catch (e) {
alert("If you change your mind, refresh your page and select 'yes' to download excel.");
return;
}
appExcel.Visible = true;
appExcel.Workbooks.Add().Worksheets.Item(1).Paste();
appExcel = null;
}
jsp代码
<a href="javascript:void(0);" οnclick="javascript:ExportToExcel()">
<img src="${pageContext.request.contextPath}/style/images/excel6.jpg" width=20px height=20px style="padding-top:15px"/>
</a>