标签:
Bean转Excel对象
1 /* 2 * 文件名:BeanToExcel.java 3 */ 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import jxl.Sheet; 9 import jxl.write.WritableCell; 10 import jxl.write.WritableSheet; 11 import jxl.write.WriteException; 12 import jxl.write.biff.RowsExceededException; 13 14 15 16 /** 17 * TODO 添加类的一句话简单描述。 18 */ 19 public class BeanToExcel<T> { 20 21 private WritableSheet wrightsheet; 22 private Sheet readSheet; 23 private List<T> beanList; 24 private Mapper<T> mapper; 25 private List<WritableCell> needChangeCells = new ArrayList<WritableCell>(); 26 27 public BeanToExcel(WritableSheet sheet, List<T> beanList, Mapper<T> mapper){ 28 this.wrightsheet = sheet; 29 this.beanList = beanList; 30 this.mapper = mapper; 31 } 32 33 public BeanToExcel(Sheet sheet, Mapper<T> mapper){ 34 this.readSheet = sheet; 35 this.mapper = mapper; 36 this.beanList = new ArrayList<T>(); 37 } 38 39 public void writeExcel() throws Exception{ 40 try { 41 write(); 42 } catch (RowsExceededException e) { 43 DEBUGGER.error("Failed to writeExcel", e); 44 throw e; 45 } catch (WriteException e) { 46 DEBUGGER.error("Failed to writeExcel", e); 47 throw e; 48 } 49 } 50 51 public List<T> readBeans(){ 52 for(int i=1; i<readSheet.getRows(); i++){ 53 try { 54 T bean = mapper.toBean(readSheet.getRow(i)); 55 if (bean instanceof ExcelInfo) { 56 ((ExcelInfo) bean).setRow(i); 57 ((ExcelInfo) bean).setReadSheet(readSheet); 58 } 60 if (bean != null){ 61 beanList.add(bean); 62 } 63 } catch (ExcelException e) { 64 needChangeCells.addAll(e.getCellList()); 65 } 66 } 67 return beanList; 68 } 69 70 protected void write() throws RowsExceededException, WriteException{ 71 for(int i = 0 ; i < beanList.size(); i++){ 72 T bean = beanList.get(i); 73 List<WritableCell> line = mapper.toRow(i+1, bean); 74 for(WritableCell cell : line){ 75 wrightsheet.addCell(cell); 76 } 77 } 78 } 79 80 public List<WritableCell> getNeedChangeCells() { 81 return needChangeCells; 82 } 83 84 }
Excel.java
1 /* 2 * 文件名:ExcelInfo.java 3 */ 4 5 import jxl.Sheet; 6 7 /** 8 * ExcleInfo 9 */ 10 public class ExcelInfo 11 { 12 13 private int row; 14 15 private Sheet readSheet; 16 17 public int getRow() 18 { 19 return row; 20 } 21 22 public void setRow(int row) 23 { 24 this.row = row; 25 } 26 27 public Sheet getReadSheet() 28 { 29 return readSheet; 30 } 31 32 public void setReadSheet(Sheet readSheet) 33 { 34 this.readSheet = readSheet; 35 } 36 }
ExcelException.java
1 /* 2 * 文件名:ExcelException.java 3 */ 4 5 import java.util.List; 6 7 import jxl.write.WritableCell; 8 9 /** 10 * TODO 添加类的一句话简单描述。 11 */ 12 public class ExcelException extends RuntimeException { 13 14 private static final long serialVersionUID = -3113079946804687851L; 15 16 public static ExcelException DEFAULT = new ExcelException("未知异常"); 17 18 private String msg; 19 private List<WritableCell> cellList; 20 21 public ExcelException(String msg) { 22 this.msg = msg; 23 } 24 25 public ExcelException(String msg, List<WritableCell> cellList) { 26 this.msg = msg; 27 this.cellList = cellList; 28 } 29 30 public ExcelException(String msg, StackTraceElement[] e, List<WritableCell> cellList) { 31 this.msg = msg; 32 this.cellList = cellList; 33 this.setStackTrace(e); 34 } 35 36 @Override 37 public String getMessage() { 38 return this.msg; 39 } 40 41 public List<WritableCell> getCellList() { 42 return cellList; 43 } 44 45 }
Mapper.java
1 /* 2 * 文件名:Mapper.java 3 */ 4 5 import java.util.List; 6 7 import jxl.Cell; 8 import jxl.format.Colour; 9 import jxl.write.Label; 10 import jxl.write.WritableCell; 11 import jxl.write.WritableCellFormat; 12 import jxl.write.WriteException; 13 14 15 /** 16 * TODO 添加类的一句话简单描述。 17 */ 18 public abstract class Mapper<T> { 19 20 22 23 public abstract List<WritableCell> toRow(int row,T t); 24 25 public abstract T toBean(Cell[] rowCells) throws ExcelException; 26 27 public WritableCell createCell(int column, int row, String content){ 28 Label label = new Label(column, row, content); 29 return label; 30 } 31 32 public WritableCell createErrorCell(int column, int row, String content){ 33 Label label = new Label(column, row, content); 34 WritableCellFormat cellFormat = new WritableCellFormat(); 35 try { 36 cellFormat.setBackground(Colour.YELLOW); 37 label.setCellFormat(cellFormat); 38 } catch (WriteException e) { 39 DEBUGGER.error("Failed to createErrorCell", e); 40 } 41 return label; 42 }47 }
标签:
原文地址:http://www.cnblogs.com/XQiu/p/5282718.html