I wanted to know if there is a way in which I can pass an HTML table from my JSP page using the Java request object.
More precisely, this is what my table looks like..
table id="tableid"
thead
tr
th...column1 header.../td
th...column2 header.../td
/tr
/thead
tbody
tr td...column1 data../td td..column2 data../td /tr /tbody/table
I've a method in my Java code getExcelVersion(), for which I wanted to pass the above table as an argument.
I actually wanted to pass data from the table(column1 data, column2 data) as an List, ArrayList or anything data type, and not the actual HTML. What I'm basically trying to achieve is something as follows:
I've an HTML table, that I need 开发者_Python百科to export to excel on a button click. For this purpose, I'm using libraries from Apache-POI, and the foll. code block.
HSSFWorkbook workbook = getExcelVersion();
resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Content-Disposition", "attachment; filename=\"contacts.xls\""); workbook.write(resp.getOutputStream());
resp.flushBuffer();
Now, I wanted to know how would I pass table data(column1 data, column2 data) to my getExcelVersion() method.
Thanks,
Pritish.Apache POI doesn't take a HTML table as input, but just fullworthy Java objects. Just reload the same data in the servlet as you loaded for the initial display in JSP and then feed it to Apache POI instead of forwarding to JSP. If you want to avoid reloading the same data, then you can consider to store it in the session scope, but this has a negative impact on server memory usage and client experience.
At least, I'm assuming that your JSP/Servlet/database code is well written, else it's going to be a lot of refactoring/rewriting work.
Related questions:
- How to avoid Java code in JSP files?
- Hidden features of JSP/Servlet
- Export to Excel (CSV) using JSP/Servlet
精彩评论