Yes you can use Apache's POI
api. There are lot of examples in the web which shows how to do this in java
. Doing this in JSF
is not very much different than that.
Visit following links to see how to do this in JSF2
.
Export to Excel JSF and PrimeFaces
How to export rich:dataTable to excel
Following example shows how to do this in jsf1.2
public void generate() {
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue("1st Col value");
row.createCell(1).setCellValue("2nd Col value");
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse res = (HttpServletResponse) context.getExternalContext().getResponse();
res.setContentType("application/vnd.ms-excel");
res.setHeader("Content-disposition", "attachment;filename=mydata.xls");
ServletOutputStream out = res.getOutputStream();
wb.write(out);
out.flush();
out.close();
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
}
Вам нужно загрузить и добавить poi-version-yyyymmdd.jar
.