donderdag, september 18, 2003

Learn to Read and Write Microsoft Excel Documents with Jakarta's POI: A Little Bit More
Some of you may need to use Java to access Excel from a Web application. In such cases, you may need to construct an Excel document and show it in a browser. That's not difficult but does require a little trick. You send the Excel document as the response, but to make it appear in Excel, you need to set the MIME type of the response to the appropriate type. This method is browser independent. Here's an example:


try {
OutputStream out = res.getOutputStream();

// SET THE MIME TYPE
res.setContentType('application/vnd.ms-excel');

// set content dispostion to attachment in
// case the open/save dialog needs to appear
res.setHeader('Content-disposition',
'inline; filename=sample');
workBook.write(out);
out.flush();
out.close();
} catch (FileNotFoundException fne) {
System.out.println('File not found...');
} catch (IOException ioe) {
System.out.println('IOException...' +
ioe.toString());
}