• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Avneet Singh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the Other Open Source Projects forum.
 
Avneet Singh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is theoretically possible to do this on a JSP page, but not practically. Trust Williams advice, and do this in a servlet.
 
Avneet Singh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic