Hi everyone

Can sum1 tell how to export data on an excel file from the click of the button which is there on the jsp page.The Data first should appaear on the jsp page and only when the user/client clicked on button then the data get exported to the excel.

deepti

Dani AI

Generated

asked for the usual workflow and pointed toward libraries. A practical pattern is: render the preview on the JSP, and have the Export button submit to a servlet (or controller) that streams the file back with the proper headers. For quick exports use CSV or an HTML table streamed as application/vnd.ms-excel. For a true .xls/.xlsx file, use a library such as Apache POI (supports XLS and XLSX; use SXSSFWorkbook for large exports).

Minimal servlet examples:

HTML-as-Excel (fast, no external libs):

resp.setContentType("application/vnd.ms-excel");
resp.setHeader("Content-Disposition","attachment; filename=\"export.xls\"");
PrintWriter out = resp.getWriter();
out.println("<table><tr><td>Col1</td><td>Col2</td></tr>");
// loop rows
out.println("</table>");
out.flush();

Apache POI (real Excel file):

Workbook wb = new HSSFWorkbook(); // HSSFWorkbook = .xls, XSSFWorkbook = .xlsx
Sheet s = wb.createSheet("Sheet1");
// create rows/cells...
wb.write(response.getOutputStream());
wb.close();

Notes and troubleshooting: ensure no JSP text is sent before headers (use a servlet), choose CSV + UTF-8 BOM if Excel must read Unicode (response.getOutputStream().write(new byte[]{(byte)0xEF,(byte)0xBB,(byte)0xBF});), and test with target Excel versions. For large datasets prefer streaming APIs (POI SXSSF) to avoid OutOfMemoryError. For reference and docs, Apache POI is a reliable starting point.

Recommended Answers

All 3 Replies

JExcel or POI

Google those APIs

Hi

Can u pls provide the source code for that....

No. Both of those APIs (you did Google them, right?) come with plenty of examples.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.