导出到Excel(Java List,HtmlDataTable,rich:dataTable)

时间:2011-05-20 05:42:05

标签: java jsf richfaces

是否有一些简单的方法如何将Java List(或rich:dataTable或htmlDataTable,基于dataList)导出到格式化的excel表?

datalist示例:

private List<BDE> dataList;
session = DaoSF.getSessionFactory('R').openSession();   
Criteria criteria = session.createCriteria(BDE.class);
  {some restrictions...}
dataList = criteria.list();

我使用AnalysisTable将其导出到csv,但似乎没有太多用户友好的导出..

谢谢

更新:我找到了primefaces解决方案,它确实有效。其余的事情是,要创建简单的格式化xls表,你有没有一些提示

1 个答案:

答案 0 :(得分:0)

    private List<BDE> dataList;
session = DaoSF.getSessionFactory('R').openSession();   
Criteria criteria = session.createCriteria(BDE.class);
  {some restrictions...}
dataList = criteria.list();

//Excel creation after your dataList has been generated 
FileOutputStream fos = new FileOutputStream("sth.xls");
//Give your file path may be to desktop so that you can see
org.apache.poi.hssf.usermodel.HSSFWorkbook workBook = new HSSFWorkbook();
Row row = null;
Cell cell = null;
Sheet sheet1 = workBook.createSheet();
    for(int rowNo = 0;rowNo<dataList.size;rowNo++){
     row= sheet1.createRow(rowNo);
      for(int cellNo = 0;cellNo <2;cellNo++){//It would go to 0th and 1st row
        cell = row.createCell(cellNo);
            cell.setCellValue("Some of your value");
            }
    }