浏览 2588 次
锁定老帖子 主题:zk导出excel
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-01-14
最后修改:2010-12-14
row javabean public class Row { private List<Column> columnList= new ArrayList(); public List<Column> getColumnList() { return columnList; } public void setColumnList(List<Column> columnList) { this.columnList = columnList; } public void appendColumn(Column column){ columnList.add(column); } } column javabean public class Column { private int columnNum; private String columnLabel; public int getColumnNum() { return columnNum; } public void setColumnNum(int columnNum) { this.columnNum = columnNum; } public String getColumnLabel() { return columnLabel; } public void setColumnLabel(String columnLabel) { this.columnLabel = columnLabel; } } sheet javabean public class Sheet { private List<Row> rowList = new ArrayList(); private String name; public List<Row> getRowList() { return rowList; } public void setRowList(List<Row> rowList) { this.rowList = rowList; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void appendRow(Row row){ rowList.add(row); } } Excel后台类 public class Excel{ public static void write(List<Sheet>sheetList,OutputStream os){ jxl.write.WritableWorkbook wwb; jxl.write.WritableFont wf1=new jxl.write.WritableFont(WritableFont.ARIAL,20,WritableFont.BOLD,false, UnderlineStyle.DOUBLE_ACCOUNTING,jxl.format.Colour.BRIGHT_GREEN); jxl.write.WritableCellFormat wff1=new jxl.write.WritableCellFormat(wf1); try { // wff1.setBackground(jxl.format.Colour.VIOLET2); // wff1.setBackground(jxl.format.Colour.VIOLET2); wwb = Workbook.createWorkbook(os); int i = 0; for(Iterator<Sheet>itr = sheetList.iterator();itr.hasNext();){ Sheet sheet = itr.next(); int rowNum =0; jxl.write.WritableSheet ws = wwb.createSheet(sheet.getName(),i); for(Iterator<Row>sheetItr = sheet.getRowList().iterator();sheetItr.hasNext();){ Row row = sheetItr.next(); int colunNum = 0; for(Iterator<Column>rowIterator=row.getColumnList().iterator();rowIterator.hasNext();){ Column column = rowIterator.next(); ws.addCell(new jxl.write.Label(colunNum,rowNum,column.getColumnLabel(),wff1)); colunNum++; } rowNum++; } i++; } //写入Exel工作表 wwb.write(); //关闭Excel工作薄对象 wwb.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 实现导出excel的方法,在前台加一个按钮调用onAdd2()方法 public void onAdd2(ForwardEvent event) throws Exception{ Excel excel=new Excel(); if(listbox.getSelectedItem()==null){ Messagebox.show("请选择要导出报表的内容!", "提示信息", Messagebox.OK, Messagebox.INFORMATION); } else{ Date dat=new Date(); //dat.setTime(dat.getTime()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String timeStr = sdf.format(dat); FileSystemView fsv = FileSystemView.getFileSystemView(); fsv.getHomeDirectory(); File f=new File(fsv.getHomeDirectory()+"//"+timeStr+ ""+listbox.getSelectedItem().getLabel()+".xls"); Sheet sheet = new Sheet(); listbox.getSelectedItem().getValue(); System.out.println(listbox.getSelectedItem().getLabel()); sheet.setName(listbox.getSelectedItem().getLabel()+"的费用报表"); Set set = listbox.getSelectedItems(); for(Iterator iter= set.iterator();iter.hasNext();) { Row row = new Row(); Listitem listitem = (Listitem) iter.next(); List columnList = new ArrayList(); for(int i = 0 ; i <listitem.getChildren().size();i++) { Column column = new Column(); column.setColumnLabel(((Listcell)listitem.getChildren().get(i)).getLabel()); column.setColumnNum(i); columnList.add(column); } row.setColumnList(columnList); sheet.appendRow(row); } List list = new ArrayList(); list.add(sheet); new Excel().write(list,new FileOutputStream(f)); Messagebox.show("导出excel成功", "提示信息", Messagebox.OK, Messagebox.INFORMATION); } } //appendTitleRow private Row appendTitleRow(Sheet sheet){ Row row = new Row(); List columnList = new ArrayList(); for(int i = 0 ; i <exportTitles.length;i++) { Column column = new Column(); column.setColumnLabel(exportTitles[i]); column.setColumnNum(i); column.setType("title"); row.appendColumn(column); } sheet.appendRow(row); return row; } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |