Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

java- poi

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
can anybody tell how to open/save/cancel a excel file.
i want a pop up in which the pop up should contain open/save/cancel a excel sheet.Am using POI and getOutputStream.
please help.
thanks
Shabarish
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you setting the content type and Content-Disposition headers? Something like
 
shabarish vai
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
POIFSFileSystem templateFs = new POIFSFileSystem(new FileInputStream("C:\\template.xls"));
HSSFWorkbook wb = new HSSFWorkbook(templateFs,true);
/*POIFSFileSystem outputfs = new POIFSFileSystem(new FileInputStream("C:workbook.xls"));
HSSFWorkbook wb_output = new HSSFWorkbook(outputfs,true);*/
HSSFSheet sheet_output = wb.getSheetAt(0);
for (Iterator rit = sheet_output.rowIterator(); rit.hasNext(); )
{
HSSFRow rowFor = (HSSFRow)rit.next();
for (Iterator cit = rowFor.cellIterator(); cit.hasNext(); )
{
HSSFCell cellFor = (HSSFCell)cit.next();
for(int i=0;i<key.length;i++)
{
String temp = cellFor.getStringCellValue();
if(temp.equalsIgnoreCase(key[i]))
{
temp=value[i];
cellFor = rowFor.createCell((short)1);
cellFor.setCellValue(temp);
}
}
}
}
String dirName ="C utput.xls";
FileOutputStream fileOut = new FileOutputStream(dirName);
wb.write(fileOut);
fileOut.close();

Originally posted by Ulf Dittmer:
Are you setting the content type and Content-Disposition headers? Something like

 
shabarish vai
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shabarish vai:
POIFSFileSystem templateFs = new POIFSFileSystem(new FileInputStream("C:\\template.xls"));
HSSFWorkbook wb = new HSSFWorkbook(templateFs,true);
/*POIFSFileSystem outputfs = new POIFSFileSystem(new FileInputStream("C:workbook.xls"));
HSSFWorkbook wb_output = new HSSFWorkbook(outputfs,true);*/
HSSFSheet sheet_output = wb.getSheetAt(0);
for (Iterator rit = sheet_output.rowIterator(); rit.hasNext(); )
{
HSSFRow rowFor = (HSSFRow)rit.next();
for (Iterator cit = rowFor.cellIterator(); cit.hasNext(); )
{
HSSFCell cellFor = (HSSFCell)cit.next();
for(int i=0;i<key.length;i++)
{
String temp = cellFor.getStringCellValue();
if(temp.equalsIgnoreCase(key[i]))
{
temp=value[i];
cellFor = rowFor.createCell((short)1);
cellFor.setCellValue(temp);
}
}
}
}
String dirName ="C utput.xls";
FileOutputStream fileOut = new FileOutputStream(dirName);
wb.write(fileOut);
fileOut.close();
openTemplate(dirName,response);

}
/**
* @param dirName
* @param response
* @throws Exception
*/
private void openTemplate(String dirName, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
File file = new File(dirName);
byte[] bytes = getBytesFromFile(file);
BufferedOutputStream bou = new BufferedOutputStream(response.getOutputStream());
response.setHeader("Content-Disposition","attachment;filename=workbook.xls");
response.setContentType("application/vnd.ms-excel");
bou.write(bytes);

 
shabarish vai
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when i generate the pop up the contents wil not be copied properly if i open excel sheet from pop up i'll get error message in excel sheet telling that file is damaged

Originally posted by Ulf Dittmer:
Are you setting the content type and Content-Disposition headers? Something like



 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to stream the file to the client, you don't need to write it to disk first. You can use POIFSFileSystem.writeFilesystem method to write it to the output stream directly. That would eliminate possible sources of problems (and be faster to begin with).
 
shabarish vai
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

HI, can you please explain me with few snippets
it wil be helpfull
thanks
Shabarish

Originally posted by Ulf Dittmer:
If you want to stream the file to the client, you don't need to write it to disk first. You can use POIFSFileSystem.writeFilesystem method to write it to the output stream directly. That would eliminate possible sources of problems (and be faster to begin with).

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't much to it. It would go something like this:


Also, please be a bit more careful with the CODE and QUOTE buttons. Somehow your posts contain too many of those where they aren't needed. There's also no need to quote the previous post in a response, unless you're referring to some specific part of it (in which case only that part should be quoted).
[ July 03, 2008: Message edited by: Ulf Dittmer ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic