I've a HTML table on my JSP page, that I want to be exported to Excel on a button click.
What would be the best way of going about this ?
(For ex., how would I do this using may be a jQuery function.)
我在我的JSP页面上有一个HTML表格,我想要被导出到一个按钮点击。最好的办法是什么?(对于ex,我如何使用可能是jQuery函数。)
Any code examples for demo. purposes should be great.
任何演示的代码示例。目的应该是巨大的。
Thanks,
Pritish.
谢谢,Pritish。
9 个解决方案
#1
6
I would recommend Apache POI, we've been using it for years, never had any problems.
我推荐Apache POI,我们已经使用它很多年了,从来没有任何问题。
Alot of examples online to get a good start, and the documentation on the site is also good: http://poi.apache.org/spreadsheet/quick-guide.html
网站上的文档也不错:http://poi.apache.org/spreadsheet/quick-guide.html
#2
4
Rather export to CSV format. It's supported by Excel as well. Exporting to a fullworthy XLS(X) format using for example Apache POI HSSF or JExcepAPI is slow and memory hogging.
而是导出到CSV格式。它也受到Excel的支持。例如,使用Apache POI HSSF或JExcepAPI导出到完全合格的XLS(X)格式是很慢的,并且会占用内存。
Exporting to CSV is relatively simple. You can find a complete code example in this answer.
导出到CSV是相对简单的。您可以在这个答案中找到一个完整的代码示例。
As to exporting to files using JavaScript, this is not possible without interaction of Flash or the server side. In Flash there's as far only the Downloadify library which can export to simple txt files. Further, ExtJs seems to have a CSV export library, but I can't find any feasible demo page.
至于使用JavaScript导出文件,没有Flash或服务器端交互是不可能的。在Flash中,只有Downloadify库可以导出到简单的txt文件。此外,ExtJs似乎有一个CSV导出库,但是我找不到任何可行的演示页面。
#3
3
you can parse the table using library like http://jsoup.org/
可以使用类似于http://jsoup.org/的库解析表
after you get the data , you can store it in Excel-compatible format ( CSV ) , or using java excel library for that like POI, or using JDBC to write data into excel sheet, see this example: Password Protected Excel File
在获得数据之后,您可以将其存储为与excel兼容的格式(CSV),或者使用java excel库(如POI),或者使用JDBC将数据写入excel表,请参见这个示例:密码保护excel文件
#4
1
I also spend lot of time to convert html to excel after lot of R & D i found following easiest way.
我还花了大量的时间将html转换成excel,之后我发现了很多最简单的方法。
-
create hidden field and in that pass your html data to your servlet or controller for e.g
创建隐藏字段,并将html数据传递给servlet或控制器
<form id="formexcel" action="" method="post" name="formexcel"> <input type="hidden" name="exceldata" id="exceldata" value="" /> </form>
-
on your button of href click call following function and pass your html data using in document.formexcel.exceldata.value and your servlet or controller in document.formstyle.action
在href按钮上单击call follow函数,并使用document. formexceldata传递html数据。在document.formstyle.action中为您的servlet或控制器赋值
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(); }
-
Now in your controller or servlet write following code
现在在您的控制器或servlet中编写以下代码
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());
#5
1
Excel can load CSV (comma-separated value) files, which are basically just files with everything that would go into separate Excel cells separated by comma.
Excel可以加载CSV(逗号分隔的值)文件,这些文件基本上只是文件,其中的所有内容都将进入用逗号分隔的独立Excel单元。
I don't know enough about how jQuery can handle pushing information into a file that you would download, but it seems a jQuery library has been written that at least transforms html tables to CSV format, and it is here: http://www.kunalbabre.com/projects/table2CSV.php
我不太清楚jQuery如何将信息推送到要下载的文件中,但似乎已经编写了jQuery库,至少可以将html表转换为CSV格式,它在这里:http://www.kunalbabre.com/projects/table2CSV.php
Edit (February 29, 2016): You can use the table2csv implementation above in conjunction with FileSaver.js (which is a wrapper for the HTML5 W3C saveAs()
spec).
编辑(2016年2月29日):您可以将上面的table2csv实现与FileSaver一起使用。js(它是HTML5 W3C saveAs()规范的包装)。
The usage will end up looking something like:
这个用法最终会是这样的:
var resultFromTable2CSV = $('#table-id').table2CSV({delivery:'value'});
var blob = new Blob([resultFromTable2CSV], {type: "text/csv;charset=utf-8"});
saveAs(blob, 'desiredFileName.csv');
#6
0
Exporting to Excel file format with JQuery is impossible.
用JQuery导出到Excel文件格式是不可能的。
You can try with Java. There are a lot of libraries to do that.
您可以尝试使用Java。有很多库可以做到这一点。
#7
0
You would have to create something on the server-side (like a servlet) to read the html and create the excel file and serve it back to the user.
您必须在服务器端(如servlet)创建一些东西来读取html并创建excel文件并将其返回给用户。
You could use this library to help you do the transformation.
您可以使用这个库来帮助您进行转换。
#8
0
I can suggest you to try http://code.google.com/p/gwt-table-to-excel/, at least the server part.
我建议您试试http://code.google.com/p/gwt-table-to excel/,至少是服务器部分。
#9
0
I have been using the jQuery plugin table2excel. It works very well and no serverside coding is needed.
我一直在使用jQuery插件table2excel。它工作得很好,不需要服务器端编码。
Using it is easy. Simply include jQuery
使用它是很容易的。仅仅包括jQuery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Now include the table2excel script (Remember to change the src destination to match yours)
现在包含table2excel脚本(请记住将src目的地更改为与您的相匹配)
<script src="dist/jquery.table2excel.min.js"></script>
Now simply call the script on the table you want exportet.
现在只需调用您想要exportet的表上的脚本。
$("#yourHtmTable").table2excel({
exclude: ".excludeThisClass",
name: "Worksheet Name",
filename: "SomeFile" //do not include extension
});
It's also easy to attach to a button like so:
它也很容易连接到这样的按钮:
$("button").click(function(){
$("#table2excel").table2excel({
// exclude CSS class
exclude: ".noExl",
name: "Excel Document Name"
});
});
All examples are taken directly from the authors github page and from jqueryscript.net
所有示例都直接取自authors github页面和jqueryscript.net
#1
6
I would recommend Apache POI, we've been using it for years, never had any problems.
我推荐Apache POI,我们已经使用它很多年了,从来没有任何问题。
Alot of examples online to get a good start, and the documentation on the site is also good: http://poi.apache.org/spreadsheet/quick-guide.html
网站上的文档也不错:http://poi.apache.org/spreadsheet/quick-guide.html
#2
4
Rather export to CSV format. It's supported by Excel as well. Exporting to a fullworthy XLS(X) format using for example Apache POI HSSF or JExcepAPI is slow and memory hogging.
而是导出到CSV格式。它也受到Excel的支持。例如,使用Apache POI HSSF或JExcepAPI导出到完全合格的XLS(X)格式是很慢的,并且会占用内存。
Exporting to CSV is relatively simple. You can find a complete code example in this answer.
导出到CSV是相对简单的。您可以在这个答案中找到一个完整的代码示例。
As to exporting to files using JavaScript, this is not possible without interaction of Flash or the server side. In Flash there's as far only the Downloadify library which can export to simple txt files. Further, ExtJs seems to have a CSV export library, but I can't find any feasible demo page.
至于使用JavaScript导出文件,没有Flash或服务器端交互是不可能的。在Flash中,只有Downloadify库可以导出到简单的txt文件。此外,ExtJs似乎有一个CSV导出库,但是我找不到任何可行的演示页面。
#3
3
you can parse the table using library like http://jsoup.org/
可以使用类似于http://jsoup.org/的库解析表
after you get the data , you can store it in Excel-compatible format ( CSV ) , or using java excel library for that like POI, or using JDBC to write data into excel sheet, see this example: Password Protected Excel File
在获得数据之后,您可以将其存储为与excel兼容的格式(CSV),或者使用java excel库(如POI),或者使用JDBC将数据写入excel表,请参见这个示例:密码保护excel文件
#4
1
I also spend lot of time to convert html to excel after lot of R & D i found following easiest way.
我还花了大量的时间将html转换成excel,之后我发现了很多最简单的方法。
-
create hidden field and in that pass your html data to your servlet or controller for e.g
创建隐藏字段,并将html数据传递给servlet或控制器
<form id="formexcel" action="" method="post" name="formexcel"> <input type="hidden" name="exceldata" id="exceldata" value="" /> </form>
-
on your button of href click call following function and pass your html data using in document.formexcel.exceldata.value and your servlet or controller in document.formstyle.action
在href按钮上单击call follow函数,并使用document. formexceldata传递html数据。在document.formstyle.action中为您的servlet或控制器赋值
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(); }
-
Now in your controller or servlet write following code
现在在您的控制器或servlet中编写以下代码
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());
#5
1
Excel can load CSV (comma-separated value) files, which are basically just files with everything that would go into separate Excel cells separated by comma.
Excel可以加载CSV(逗号分隔的值)文件,这些文件基本上只是文件,其中的所有内容都将进入用逗号分隔的独立Excel单元。
I don't know enough about how jQuery can handle pushing information into a file that you would download, but it seems a jQuery library has been written that at least transforms html tables to CSV format, and it is here: http://www.kunalbabre.com/projects/table2CSV.php
我不太清楚jQuery如何将信息推送到要下载的文件中,但似乎已经编写了jQuery库,至少可以将html表转换为CSV格式,它在这里:http://www.kunalbabre.com/projects/table2CSV.php
Edit (February 29, 2016): You can use the table2csv implementation above in conjunction with FileSaver.js (which is a wrapper for the HTML5 W3C saveAs()
spec).
编辑(2016年2月29日):您可以将上面的table2csv实现与FileSaver一起使用。js(它是HTML5 W3C saveAs()规范的包装)。
The usage will end up looking something like:
这个用法最终会是这样的:
var resultFromTable2CSV = $('#table-id').table2CSV({delivery:'value'});
var blob = new Blob([resultFromTable2CSV], {type: "text/csv;charset=utf-8"});
saveAs(blob, 'desiredFileName.csv');
#6
0
Exporting to Excel file format with JQuery is impossible.
用JQuery导出到Excel文件格式是不可能的。
You can try with Java. There are a lot of libraries to do that.
您可以尝试使用Java。有很多库可以做到这一点。
#7
0
You would have to create something on the server-side (like a servlet) to read the html and create the excel file and serve it back to the user.
您必须在服务器端(如servlet)创建一些东西来读取html并创建excel文件并将其返回给用户。
You could use this library to help you do the transformation.
您可以使用这个库来帮助您进行转换。
#8
0
I can suggest you to try http://code.google.com/p/gwt-table-to-excel/, at least the server part.
我建议您试试http://code.google.com/p/gwt-table-to excel/,至少是服务器部分。
#9
0
I have been using the jQuery plugin table2excel. It works very well and no serverside coding is needed.
我一直在使用jQuery插件table2excel。它工作得很好,不需要服务器端编码。
Using it is easy. Simply include jQuery
使用它是很容易的。仅仅包括jQuery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Now include the table2excel script (Remember to change the src destination to match yours)
现在包含table2excel脚本(请记住将src目的地更改为与您的相匹配)
<script src="dist/jquery.table2excel.min.js"></script>
Now simply call the script on the table you want exportet.
现在只需调用您想要exportet的表上的脚本。
$("#yourHtmTable").table2excel({
exclude: ".excludeThisClass",
name: "Worksheet Name",
filename: "SomeFile" //do not include extension
});
It's also easy to attach to a button like so:
它也很容易连接到这样的按钮:
$("button").click(function(){
$("#table2excel").table2excel({
// exclude CSS class
exclude: ".noExl",
name: "Excel Document Name"
});
});
All examples are taken directly from the authors github page and from jqueryscript.net
所有示例都直接取自authors github页面和jqueryscript.net