Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Help needed in Excel download



Hi all,
Am trying to download an excel kept in the server by java servlet. on click of download button, download dialog box appears and on clicking on save the file gets downloaded and saved successfully. Everything in this is perfect. But on click of open button i want my excel to be opened in separate excel instead of opening the excel as embedded in the browser itself. Any guidelines would help me a lot.. here is the code.. am passing the file name from jsp and getting the name in this servlet.

Should i have to use jexcel for writing into excel separately and pop up on click of open?? If so, can anyone explain with an example?

public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String filename = request.getParameter("fileName");
String fileDisplayName = filename.substring(filename.lastIndexOf("/")+1);
String dataFileLocation = "C:\"
File f = new File(dataFileLocation+"\\"+filename);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="+fileDisplayName);
response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "no-store");
        response.addHeader("Cache-Control", "max-age=0");
FileInputStream fin = null;
try {
fin = new FileInputStream(f);
} catch (FileNotFoundException e) {
      e.printStackTrace();
}
int size = 1024;
try {
response.setContentLength(fin.available());
byte[] buffer = new byte[size];
ServletOutputStream os = null;

os = response.getOutputStream();
int length = 0;
while ((length = fin.read(buffer)) != -1) {
        os.write(buffer, 0, length);
      
}
fin.close();
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}

}