0

I have used jxls.jar library to export data in to excel format and stored in file with *.xls format. How can I open or promote dialog box for open or save this file after complete writing process into file using servlet the all proccess for writing in to file is done in separate function..

Petteri H
  • 11,779
  • 12
  • 64
  • 94
Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50

4 Answers4

2

I understand that you have the Excel file as a File object and that you want to provide this as a download to the client. You need to set the Content-Disposition header to attachment to let the client show a Save As dialogue. You also need to set the Content-Type header as well to let the client know what file type it is so that it can eventually associate the right application with it for the case that the enduser would like to open it immediately. Finally, setting the Content-Length header is preferably as it improves serving performance (otherwise the Servlet API will fall back to chunked encoding which requires a bit more bytes and processing time).

After setting the proper headers, it's just a matter of writing the InputStream from the File to the OutputStream of the HttpServletResponse the usual Java IO way.

private static final int DEFAULT_BUFFER_SIZE = 8192; // 8KB.
// ...

File file = createExcelFileSomehow();
// ...

response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setHeader("Content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");

BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
    input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
    output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    for (int length; (length = input.read(buffer)) > -1;) {
        output.write(buffer, 0, length);
    }
} finally {
    if (output != null) try { output.close(); } catch (IOException ignore) {}
    if (input != null) try { input.close(); } catch (IOException ignore) {}
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Are you asking how to send the file to the user?

This may help: Servlet for serving static content

Then just create an HTML link to the servlet from whatever you use for presentation.

Community
  • 1
  • 1
Dmitri
  • 8,999
  • 5
  • 36
  • 43
0

Add a header Content-Disposition: attachment

Amareswar
  • 2,048
  • 1
  • 20
  • 36
0

This code snippet should help you. When you give Content disposition as inline in the IE browsers it will open the excel as such without prompting the dialog box.

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","inline;fileName=" + fileName);
final java.io.OutputStream os = response.getOutputStream();

call the createExcel function passing OutputStream Object

 
os.flush();
os.close();
Balaji.N.S
  • 745
  • 3
  • 13
  • 28