0

I have developed a web application using JSP which creates a page with several page elements and a with multiple records. I wanted to export the content in that into an excel file.

I used the code

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="
        + "excel.xls");

as described in this article http://www.quicklyjava.com/export-web-page-to-excel/

But it exports the whole page. How can a export only the content in that particular table?

I cannot use the Apache POI library since i have to format(text colors,cell colors) the content in the JSP. And i need the same formatting in the excel file.

Can somebody help me with this? :)

direndd
  • 642
  • 2
  • 16
  • 47
  • You can just *move* the table into a new page and set the content type to `application/vnd.ms-excel` in it. – Luiggi Mendoza Jan 19 '13 at 13:22
  • see http://stackoverflow.com/questions/5524143/how-can-i-export-tables-to-excel-from-a-webpage – Rachel Gallen Jan 19 '13 at 14:14
  • @Luiggi : I have to keep the other elements in the page. Cannot use a separate page with just the table. It's against the requirement. :( – direndd Jan 19 '13 at 17:59
  • You asked to *export* the table content into an Excel file, so you can use a button i.e. `` to call the page with the Excel content type and the table content, nothing else. In this way, your initial page keeps intact and your new page contains the same table/data. – Luiggi Mendoza Jan 19 '13 at 18:27
  • @Rachel : I read that question but it doesn't have the answer for exporting only the content of a table. – direndd Jan 19 '13 at 18:31
  • @Luiggi: I'm new to JSP. Can you explain it more?It would be a great help! :) – direndd Jan 19 '13 at 18:37
  • Before to post an answer, please tell me: 1) Are you using any MVC Framework like Spring 3, Struts 2, JSF 2 or other or plain JSP/Servlets? 2) Are you using jQuery or another js library in your project/web pages? – Luiggi Mendoza Jan 19 '13 at 18:40
  • I'm using Struts 2. Yes there are few js libraries(including jQuery) i use in my project. – direndd Jan 19 '13 at 18:45
  • Related: http://stackoverflow.com/questions/1763964/jsp-response-content-type-excel/1764035#1764035 In other words, don't fool Excel. Just do it the right way. – BalusC Jan 19 '13 at 22:34
  • @BalusC: Thankxx! I tried Apache POI HSSF but i think it doesn't allow formatiing the cells(font colors,Cell colours etc) does it? – direndd Jan 20 '13 at 16:24
  • Yup, POI will happily let you format the text, control the colours and fonts etc – Gagravarr Jan 21 '13 at 17:20
  • Thank you @Gagravarr POI HSSF library let us do most of the formatting to the excel sheet. http://poi.apache.org/spreadsheet/how-to.html#user_api Examples http://poi.apache.org/spreadsheet/examples.html – direndd Jan 29 '13 at 13:16

1 Answers1

0

Haven't found a way to export only a single table in a page. But the intended task(create a excel sheet with formatted data) can be done by Apache POI HSSF library

Here are the links to learn how to & examples - http://poi.apache.org/spreadsheet/how-to.html#user_api

http://poi.apache.org/spreadsheet/examples.html

direndd
  • 642
  • 2
  • 16
  • 47