File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Open Source Projects and the fly likes JSP - POI project - facing problem while writing excel sheet  from jsp. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "JSP - POI project - facing problem while writing excel sheet  from jsp." Watch "JSP - POI project - facing problem while writing excel sheet  from jsp." New topic
Author

JSP - POI project - facing problem while writing excel sheet from jsp.

Avneet Singh
Ranch Hand

Joined: Apr 25, 2006
Posts: 51
Hi

I have been using POI project with core java and it works just fine.
But when i try to use it in jsp code my excel sheet is blank.

The code below is what works fine with java - remove the commect for core java below to run it.

im providin a dummy code -

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","attachment;filename=Web_Transact_Report.xls");

int rownum= 0 ;
HSSFWorkbook wb= new HSSFWorkbook();
HSSFSheet sheet= wb.createSheet("Transaction Report");
HSSFSheet sheet1= wb.createSheet("Net Banking");
HSSFRow row = sheet.createRow((short)rownum);
rownum++;
HSSFCellStyle headerStyle= wb.createCellStyle();
HSSFFont headerfont= wb.createFont();
headerfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerfont.setColor((short)9);
headerfont.setFontName("Arial");
headerStyle.setFont(headerfont);
headerStyle.setFillBackgroundColor((short)18);
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFCellcel ;
cel = row.createCell((short)0);
cel.setCellValue("Service");
cel.setCellStyle(headerStyle);
cel = row.createCell((short)1);
cel.setCellValue("TXN ID");
cel.setCellStyle(headerStyle);

/* this line is used with java code*/
//FileOutputStream file = new FileOutputStream("Web_Transact_Report.xls");

/* this line below is used for jsp output stream and gives me a message that this out put stream has already been called for this response*/

wb.write(response.getOutputStream());

/*used with java code*/
//file.close();

The problem is if i use response.getOutputStream() the jsp gives error output stream already called. The above code is all my jsp page has except the directive.

If i use the conventional way of out put stream then on jsp it gives a blank excelsheet but works fine with normal code.

Need some urgent help .
Thanks
Avneet
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
You should be handling this in a servlet, not a JSP. JSPs have so many assumptions about output of HTML. In this case your JSP is getting the output stream and writing a bunch of output before it gets to your code.

Bill
Aravind Prasad
Ranch Hand

Joined: Dec 28, 2005
Posts: 263
If you want to get the values from a jsp page to an excel sheet..
you don't want to write these much lines..
it can be fixes with 2 lines.. itself

if u want the code for that program
i can send to u now

regards
Aravind
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Moved to the Other Open Source Projects forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Avneet Singh
Ranch Hand

Joined: Apr 25, 2006
Posts: 51
Hey

So what you are sayin is do this with servelet, but im sure this could be implemented on a jsp page to.Just need to fix this output stream already called issue.Would apreciate some more help on this.



Aravind you have some dummy code can i have a look .
Doing this the table way is not good because then i cant format my excel sheet.

Thanks Avneet
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41038
    
  43
It is theoretically possible to do this on a JSP page, but not practically. Trust Williams advice, and do this in a servlet.


Ping & DNS - my free Android networking tools app
Avneet Singh
Ranch Hand

Joined: Apr 25, 2006
Posts: 51
Wel im gonna try this on a servlet, but i would like to know why this reponse already been set or getOutputStream() for this response is already been used comes even when im nt using the response object any where else.
except setting the header of the page.

Thanks
Avneet
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41038
    
  43
That's a questions about how JSP works. You might have a look at the Java source code that gets produced from the JSP page (if you're using Tomcat it's somewhere deep in the "work" directory). That should show you how the stream is used in places other than the ones where you use it.
Avneet Singh
Ranch Hand

Joined: Apr 25, 2006
Posts: 51
thanks william and ulf for your valuable suggestions i have been able to implement the poi and create sheet thru servlets.

Thanks for your help guys.
Nazim Bougherara
Greenhorn

Joined: Aug 30, 2006
Posts: 1
Hello Avneet,
I m new commer in the ranch ;-)
could you please send me any example code of excel export using poi (jsp or servlet)

Many thanks.
nageshwar reddy
Greenhorn

Joined: Sep 14, 2009
Posts: 2
hi all,
can any one provide the code for sending JSP data(which is From DataBase) as well as the API used for it.And Where to place the API in my project directory.
 
jQuery in Action, 2nd edition
 
subject: JSP - POI project - facing problem while writing excel sheet from jsp.
 
Similar Threads
how to extract data from html page to excel sheet
apache pio?
Generate Excel report in java
How to make POI HSSF support Chinese Encoding?
how to extract data to an xl sheet