经过改良后可以导出超过70000条数据的导出公共excel类
public static <T> void export(OutputStream os, List<T> obj, String[] head,String[] fidldName)
throws Exception {
// 创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(os);
double num =Math.ceil(obj.size()/65000d);
if (num>1) {
for (int i =0; i < num; i++) {
List<T> objj=new ArrayList<T>();
int max=0;
int k=0;
if (obj.size()<=65536) max=obj.size();
if (obj.size()>65536) max=65536*i+65536;
if (num-1==i) max=obj.size();
for (int j = 65536*i; j < max; j++) {
objj.add(k, obj.get(j));
k++;
}
exportS(os,objj,head,fidldName,(int)num,workbook);
}
}else{
exportS(os,obj,head,fidldName,(int)num,workbook);
}
workbook.write();
workbook.close();
os.close();
}
public static <T> void exportS(OutputStream os, List<T> obj, String[] head,String[] fidldName,int num,WritableWorkbook workbook) throws Exception {
// 创建新的一页
WritableSheet sheet = workbook.createSheet("Sheet"+num,num);
// 创建要显示的具体内容
// String[] head = HeaderExcel.List;
for (int i = 0; i < head.length; i++) {
Label formate = new Label(i, 0, head[i] + "", titleFormat);
sheet.addCell(formate);
}
int i = 0;
Field field = null;
for (T t : obj) {
Object [] objw=new Object[50];
int k = 0;
//System.err.println(fidldName.length);
for (int j = 0; j < fidldName.length; j++) {
field = t.getClass().getDeclaredField(fidldName[j]);
field.setAccessible(true);
if(field.get(t) != null)
objw[k]=field.get(t);
else
objw[k]="-";
k++;
}
for (int j = 0; j < head.length; j++) {
Label data = new Label(j, 1 + i, "" + objw[j]);
sheet.addCell(data);
}
i++;
}
}