导出工具类
package com.lovefly.util;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.lovefly.service.api.excel.Excelable;
public class ExcelUtil<T extends Excelable> {
private WritableWorkbook book ;
private WritableSheet sheet;
private int rowIndex = 0;
List<String> attrs;
List<T> poList;
Class<T> clas;
public ExcelUtil(OutputStream out,String name,List<String> attrs,List<T> poList,Class<T> c) throws IOException {
book = Workbook.createWorkbook(out);
sheet = book.createSheet(name, 0);
this.attrs = attrs;
this.poList = poList;
clas = c;
}
private void writeBody() throws Exception {
Label label = null;
for(int i=0;i<poList.size();i++){
for(int j=0;j<attrs.size();j++){
Field field = clas.getDeclaredField(attrs.get(j));
field.setAccessible(true);
label = new Label(j,rowIndex,field.get(poList.get(i)).toString());
sheet.addCell(label);
}
rowIndex++;
}
}
private void writeTitle() throws Exception{
Label label = null;
for(int i=0;i<attrs.size();i++){
label = new Label(i,rowIndex,clas.newInstance().getAttrTitleMapping().get(attrs.get(i)));
sheet.addCell(label);
}
rowIndex++;
}
public void writeClose(){
try {
writeTitle();
writeBody();
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}