public void exportData() { FacesContext ctx = FacesContext.getCurrentInstance(); ctx.responseComplete(); HttpServletResponse response = (HttpServletResponse) ctx .getExternalContext().getResponse(); response.reset(); response.setContentType("application/vnd.ms-excel"); WritableWorkbook wwb = null; try { wwb = Workbook.createWorkbook(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } if (wwb != null) { WritableSheet ws = wwb.createSheet("sheet1", 0); for (int i = 0; i < incomeTaxList.size(); i++) { try { ws.addCell(new Label(0, i, incomeTaxList.get(i) .getSalaryStart().toString() + "~" + incomeTaxList.get(i).getSalaryEnd().toString())); ws.addCell(new Label(1, i, incomeTaxList.get(i).getTax0() .toString())); ws.addCell(new Label(2, i, incomeTaxList.get(i).getTax1() .toString())); ws.addCell(new Label(3, i, incomeTaxList.get(i).getTax2() .toString())); ws.addCell(new Label(4, i, incomeTaxList.get(i).getTax3() .toString())); ws.addCell(new Label(5, i, incomeTaxList.get(i).getTax4() .toString())); ws.addCell(new Label(6, i, incomeTaxList.get(i).getTax5() .toString())); ws.addCell(new Label(7, i, incomeTaxList.get(i).getTax6() .toString())); ws.addCell(new Label(8, i, incomeTaxList.get(i).getTax7() .toString())); ws.addCell(new Label(9, i, incomeTaxList.get(i).getTax8() .toString())); ws.addCell(new Label(10, i, incomeTaxList.get(i).getTax9() .toString())); ws.addCell(new Label(11, i, incomeTaxList.get(i).getTax10() .toString())); ws.addCell(new Label(12, i, incomeTaxList.get(i).getTax11() .toString())); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { wwb.write(); wwb.close(); } catch (IOException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } }
Java操作Excel:http://www.iteye.com/topic/55844