1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
protected ActionForward onexecute(ActionMapping _mapping, ActionForm _form, HttpServletRequest _request, HttpServletResponse _response)
throws IOException, ServletException {
HSSFWorkbook wb = new HSSFWorkbook();
// generate the Excel file and put it in the HTTP response
OutputStream outputStream = null;
try
{
outputStream = generateFile(_response, wb, "OK");
}
catch(Exception e)
{
LOGGER.error(e.getMessage(), e);
}
finally
{
if(outputStream != null)
{
outputStream.flush();
outputStream.close();
}
}
return _mapping.findForward("success");
}
protected OutputStream generateFile(final HttpServletResponse _response,
HSSFWorkbook wb,
String arreteCourantText) throws IOException
{
// stream of bytes
OutputStream outputStream;
// set the HTTP response headers
_response.setContentType("application/octet-stream");
_response.addHeader("Content-Disposition", "attachment; filename=\"monFichier.xls\"");
_response.setHeader("Pragma", "public");
_response.setHeader("Cache-Control", "cache");
_response.setHeader("Cache-control", "must-revalidate");
// put the Excel file in the HTTP response as a stream of bytes
outputStream = _response.getOutputStream();
byte[] bytes = wb.getBytes();
POIFSFileSystem fs = new POIFSFileSystem();
fs.createDocument(new ByteArrayInputStream(bytes), "Workbook");
fs.writeFilesystem(outputStream);
HSSFSheet sheet = wb.createSheet("test");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(10);
return outputStream;
}
} |
Partager