1
votes

I have a template excel in my workspace in which certain will be updated when user clicks on print button on front page.

My requirement is on clicking of print button the excel should get opened in a new tab of browser so that user can take its print but it is not opening in a browser rather it gets downloaded.I don't want user to download excel it should be 'print only'.

I have written excel code in apache poi and have set the 'content-disposition' parameter to 'inline' but still it doesn't work

FileInputStream fsIP= new FileInputStream(new File(request.getSession().getServletContext().getRealPath(".")+ "/Cheque Printing.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fsIP);

HSSFSheet worksheet = wb.getSheetAt(0);
worksheet.setDisplayGridlines(false);
HSSFCell cell = null;

cell = (HSSFCell)worksheet.getRow(1).getCell((short)8);               
cell.setCellValue(date);

cell = (HSSFCell)worksheet.getRow(2).getCell((short)2);
cell.setCellValue(payee);


cell = (HSSFCell)worksheet.getRow(3).getCell((short)3);
cell.setCellValue(amt1);


cell = (HSSFCell)worksheet.getRow(5).getCell((short)8);
cell.setCellValue(amount);

fsIP.close();
response.setContentType("application/vnd.ms-excel");
// I thought this line will help me out.    
response.setHeader("Content-Disposition", "inline; filename=filename.xls");                   

final ServletOutputStream os =  response.getOutputStream();
wb.write(os); 
os.flush();
os.close();
1
Have you tried to open excel file in a new tab of browser on your locale machine? Can you show us what are you trying to achieve? Actually, to display excel file on a web page is an entirely different problem. - default locale
if we do similar coding in itext pdf and do - JG1991
The main difference is that browser usually can open PDF files, but will launch external applications to handle XLS files. - default locale
if we do similar coding in itext pdf and do response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "inline; filename=filename.pdf"); then it will open pdf in browser - JG1991
"my main concern is to allow user to just print excel and not download". Every file served to a browser can be downloaded, even it it's displayed in the browser window by default. Also: To view excel files in a browser window, a plugin must be installed on the client's machine. I don't think such a plugin is available for Linux or Mac, so the desired feature might only work on some windows machines. - cello

1 Answers

0
votes

I've tried displaying excel in browser and gave up when I found this Open excel sheet in browser

The only solution is using Apache POI for parsing the data and server the data in JSP, JSF or simply HTML as mentioned in the above link.