首页 > 代码库 > zk实现导出Excel报表
zk实现导出Excel报表
package com.bailin.framework.util.excel;
import java.util.ArrayList;
import java.util.List;
/** * 行 * * @author cj * */ public class Row {
private List<Column> columnList = new ArrayList<Column>(); private String columnName;
public List<Column> getColumnList() { return columnList; }
public void setColumnList(List<Column> columnList) { this.columnList = columnList; }
public void appendColumn(Column column) { columnList.add(column); }
public String getColumnName() { return columnName; }
public void setColumnName(String columnName) { this.columnName = columnName; }
}
package com.bailin.framework.util.excel;
/** * 列 * @author cj * */
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; }
}
package com.bailin.framework.util.excel;
import java.util.ArrayList; import java.util.List;
/** * 页 * @author cj * */
public class Sheet {
public Sheet(){
}
public Sheet(List<Row> listRow,String name){
this.rowList = listRow; this.name = name;
} private List<Row> rowList = new ArrayList<Row>();
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); }
}
package com.bailin.framework.util.excel;
import java.io.File; import java.util.ArrayList; import java.util.List;
import javax.swing.JFileChooser;
import org.zkoss.zul.Listbox; import org.zkoss.zul.Listhead; import org.zkoss.zul.Listheader;
/** * 添加头部 * * @author cj * */ public class ExportExcel { /** * 设置头部 * @param listbox 页面listBox * @return 一行 */ public static Row addpendtitle(Listbox listbox) { Row row = new Row(); //行 List<Column> columnlist = new ArrayList<Column>();//保存所有列 Listhead s = listbox.getListhead(); //获取头部 List<Listheader> j = s.getChildren();//获取到头部子节点 for (int i = 0; i < j.size(); i++) { //遍历列 Column column = new Column(); //列 column.setColumnLabel(j.get(i).getLabel()); column.setColumnNum(i); columnlist.add(column);// 添加到列 } row.setColumnList(columnlist); row.setColumnName("表头"); return row; }
/** * 弹出选择文件保存位置 * @return 文件保存位置 */ public static String filename(){ String fileName = "d://导出Excel"; // JFileChooser dialog = new JFileChooser(); // dialog.setCurrentDirectory(new File("e://"));//设置默认打开路径 // dialog.setDialogTitle("将报表存放于"); // dialog.setFileSelectionMode(JFileChooser.FILES_ONLY); // dialog.setDialogType(JFileChooser.SAVE_DIALOG); // int result = dialog.showSaveDialog(dialog); // if(result == JFileChooser.APPROVE_OPTION){ // File file = dialog.getSelectedFile(); // fileName = file.getAbsolutePath(); //得到文件全名 // } return fileName; } }
package com.bailin.framework.util.excel;
import java.io.File; import java.io.FileOutputStream; import java.util.Iterator; import java.util.List;
import org.zkoss.zul.Filedownload;
import jxl.Workbook; import jxl.format.UnderlineStyle; import jxl.write.WritableFont; /** * 输出excel格式 * @author cj * */ public class Excel { public static boolean write(List<Sheet>sheetList,String fileName,int width){ jxl.write.WritableWorkbook wwb; jxl.write.WritableCellFormat wff1=new jxl.write.WritableCellFormat(); try { FileOutputStream os = new FileOutputStream(fileName+".xls"); //文件输出位置 wff1.setAlignment(jxl.format.Alignment.CENTRE); //设置单元格剧中 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();){ ws.setColumnView(colunNum, width);// 设置第列宽 Column column = rowIterator.next(); if(null != row.getColumnName() && row.getColumnName().equals("表头")){ jxl.write.WritableFont wf2=new jxl.write.WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); // 设置字体样式 jxl.write.WritableCellFormat wf1=new jxl.write.WritableCellFormat(wf2); wf1.setAlignment(jxl.format.Alignment.CENTRE); //设置单元格剧中 ws.addCell(new jxl.write.Label(colunNum,rowNum,column.getColumnLabel(),wf1)); }else{ ws.addCell(new jxl.write.Label(colunNum,rowNum,column.getColumnLabel(),wff1)); } colunNum++; } rowNum++; } i++; } try { wwb.write(); wwb.close(); os.close(); File file = new File(fileName+".xls"); Filedownload.save(file, "application/vnd.ms-excel"); return true; } catch (Exception e) { e.printStackTrace(); return false; } } catch (Exception e) { e.printStackTrace(); return false; } }
}
package com.bailin.framework.util.excel;
import java.io.File; import java.util.ArrayList;
import javax.swing.filechooser.FileFilter; /** * 过滤文件 * @author cj * */ public class TextFileFilter extends FileFilter { private ArrayList<String> extensions = new ArrayList<String>(); private ArrayList<String> descriptions = new ArrayList<String>(); public TextFileFilter(){ super(); } public TextFileFilter(String extension, String description) { super(); this.extensions.add(extension); this.descriptions.add(description); } @Override public boolean accept(File pathname) { if (pathname != null) { if (pathname.isDirectory()) { return true; } String extension = getExtension(pathname); for(int i=0; i<extensions.size(); i++){ if(extensions.get(i).toLowerCase().endsWith(extension.toLowerCase())){ return true; } } } return false; }
private String getExtension(File pathname) { if (pathname != null) { String filename = pathname.getName(); int i = filename.lastIndexOf(‘.‘); if (i > 0 && i < filename.length() - 1) { return filename.substring(i).toLowerCase(); } } return null; }
@Override public String getDescription() { return descriptions.get(descriptions.size()-1); } }
zk实现导出Excel报表