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 JSE/JEE APIs and the fly likes java- poi Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "java- poi" Watch "java- poi" New topic
Author

java- poi

shabarish vai
Ranch Hand

Joined: May 26, 2008
Posts: 79
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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39544
    
  27
Are you setting the content type and Content-Disposition headers? Something like


Ping & DNS - updated with new look and Ping home screen widget
shabarish vai
Ranch Hand

Joined: May 26, 2008
Posts: 79
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

Joined: May 26, 2008
Posts: 79
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

Joined: May 26, 2008
Posts: 79
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
Marshal

Joined: Mar 22, 2005
Posts: 39544
    
  27
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

Joined: May 26, 2008
Posts: 79
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
Marshal

Joined: Mar 22, 2005
Posts: 39544
    
  27
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java- poi
 
Similar Threads
Closing a pop up window of Excel on cancel.
Avoid Dialog box when downloading a file
opening an existing excel file on local
How to avoid open/Save dialog box while downloading?
Issue with IE downloading excel