Я экспортирую dataTable (связывание dataList) в файл Excel, вызывая следующий метод на xlsWorkBookPrepare("c:\\export.xls");
Часть метода:
public void xlsWorkBookPrepare(String file) throws IOException
{
/* prepare of workbook */
Workbook wb = new HSSFWorkbook();
Map<String, CellStyle> styles = Style.createStyles(wb);
...
for (FoodList item : dataList)
{
...
}
/* create file */
FileOutputStream fileOut;
try
{
fileOut = new FileOutputStream(file);
wb.write(fileOut);
fileOut.flush();
fileOut.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
Нопуть связан с сервером.Как сохранить его на стороне клиента ??
РЕШЕНИЕ (на основе ответа Ранги Лин):
HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
res.setContentType("application/vnd.ms-excel");
res.setHeader("Content-disposition", "attachment; filename=test.xls");
try
{
ServletOutputStream fileOut = res.getOutputStream();
wb.write(fileOut);
fileOut.flush();
fileOut.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
FacesContext faces = FacesContext.getCurrentInstance();
faces.responseComplete();