您可以使用像http://jsoup.org/这样的库解析表格。
在获取数据后,您可以将其存储为Excel兼容格式(CSV),或使用Java Excel库(例如POI)进行存储,或使用JDBC将数据写入Excel工作表。请参阅此示例:密码保护的Excel文件
Excel可以加载CSV(逗号分隔值)文件,这些文件基本上只是将应该放入单独Excel单元格的所有内容用逗号分隔。
我不太了解jQuery如何处理将信息推送到您要下载的文件中,但似乎已经编写了一个jQuery库,至少可以将html表格转换为CSV格式,它在这里: http://www.kunalbabre.com/projects/table2CSV.php
编辑(2016年2月29日):
您可以将上面的table2csv实现与FileSaver.js(这是HTML5 W3C saveAs()
规范的包装器)结合使用。
使用方法最终看起来会像这样:
var resultFromTable2CSV = $('#table-id').table2CSV({delivery:'value'});
var blob = new Blob([resultFromTable2CSV], {type: "text/csv;charset=utf-8"});
saveAs(blob, 'desiredFileName.csv');
我也花了很多时间将HTML转换为Excel,在经过大量研究和开发后,我发现以下是最简单的方法。
<form id="formexcel" action="" method="post" name="formexcel">
<input type="hidden" name="exceldata" id="exceldata" value="" />
</form>
function exportDivToExcel() {
document.formexcel.exceldata.value=$('#htmlbody').html();
$("#lblReportForPrint").html("Procurement operation review report");
document.formstyle.method='POST';
document.formstyle.action='${pageContext.servletContext.contextPath}/generateexcel';
document.formstyle.submit();
}
StringBuilder exceldata = new StringBuilder();
exceldata.append(request.getParameter("exceldata"));
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=\"exportexcel.xls\"");
outputStream.write(exceldata.toString().getBytes());
我一直在使用jQuery插件table2excel。它运行良好,无需服务器端编码。
使用它很容易。只需包含jQuery即可。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
现在包含table2excel脚本(记得将src目标更改为与您的匹配)
<script src="dist/jquery.table2excel.min.js"></script>
$("#yourHtmTable").table2excel({
exclude: ".excludeThisClass",
name: "Worksheet Name",
filename: "SomeFile" //do not include extension
});
同时,将其附加到按钮上也很容易:
$("button").click(function(){
$("#table2excel").table2excel({
// exclude CSS class
exclude: ".noExl",
name: "Excel Document Name"
});
});
所有示例均直接取自作者的Github页面和jqueryscript.net
使用JQuery导出Excel文件格式是不可能的。
您可以尝试使用Java。有很多库可以实现这个功能。