因为程序版本的限制,最多只能用到poi3.15,所以用不了easyexcel。小类初衷。草率的写,也没有测试什么很大量数据的情况,有问题可以指出来哟。
由于程序的中心思想是解决导出excel,所以数据是单纯的对象属性不包括list等等。分为两个步骤,一个是注释,还有一个就是工具类操作。
我的程序是测试导成本地的,如果你要用来发给前端什么的也可以另外写输出。
注释定义:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Inherited
public @interface PoiExcel {
String value();
int index();
}
其中index表示该属性对应在excel中的列数,从0开始,value表示列标题。
工具类定义:
包括以下几个部分
1.创建表头 createHeader
2.单条数据行的写入 writeDatas
3.多条数据的写入 getHSSFWorkbook
这部分主要是控制workbook里面的sheet页,表头创建以及数据的填充三个部分
4.创建表头样式 getStringCenterStyle
5.main函数测试
public class ExcelUtil{ public static HSSFWorkbook getHSSFWorkbook(HSSFWorkbook wb, List dataList, Class clazz, int pageSize) { if (wb == null) { wb = new HSSFWorkbook(); } if (dataList == null || dataList.size == 0) { createHeader(wb, String.valueOf(0), clazz); return wb; } int j = 0; int maxRow = pageSize;//sheet.getLastRowNum();65535 if (maxRow > 65535 || maxRow <=0) { maxRow = 65535; } int dataSize = dataList.size(); HSSFSheet sheet = null; for (int i = 0; i < dataSize; i++) { if (i % maxRow == 0) { sheet = createHeader(wb, String.valueOf(j++), clazz); } writeDatas(sheet, dataList.get(i), clazz, (i % maxRow + 1)); } try { if(wb != null){ wb.close(); } } catch (IOException e) { e.printStackTrace(); return null; } return wb; } public static HSSFSheet createHeader(HSSFWorkbook wb, String sheetName, Class clazz) { HSSFSheet sheet = wb.createSheet(sheetName); HSSFRow row = sheet.createRow(0); Field[] fields = clazz.getDeclaredFields(); HSSFCell cell; HSSFCellStyle style = getStringCenterStyle(wb); for (Field field : fields) { PoiExcel poiExcel = field.getAnnotation(PoiExcel.class); if (poiExcel == null){ continue; } cell = row.createCell(poiExcel.index()); cell.setCellStyle(style);//设置单元格格式为"文本" cell.setCellType(CellType.STRING); cell.setCellValue(poiExcel.value()); } return sheet; } public static void writeDatas(HSSFSheet sheet, T data, Class clazz, int rowNum) { Field[] fields = clazz.getDeclaredFields(); List fieldList = Arrays.asList(fields); HSSFRow row = sheet.createRow(rowNum); fieldList.stream().forEach(field -> { field.setAccessible(true); PoiExcel poiExcel = field.getAnnotation(PoiExcel.class); if (poiExcel != null) { String fieldVal = ""; try { fieldVal = (String) field.get(data); } catch (IllegalAccessException e) { e.printStackTrace(); } row.createCell(poiExcel.index()).setCellValue(fieldVal); } }); } public static HSSFCellStyle getStringCenterStyle(HSSFWorkbook wb) { HSSFCellStyle style = wb.createCellStyle(); HSSFDataFormat format = wb.createDataFormat(); style.setDataFormat(format.getFormat("@")); style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式 return style; } public static void main(String[] args) { List a = new ArrayList<>(); TestEntity a1 = new TestEntity(); a1.setT1("1"); a1.setT2("2"); TestEntity a2 = new TestEntity(); a2.setT1("1"); a2.setT2("2"); a.add(a1); a.add(a2); HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(null, a, TestEntity.class, 1); FileOutputStream fos = null; try { fos = new FileOutputStream(new File("F://test.xls")); wb.write(fos); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }
测试用的entity如下:
@Component
public class TestEntity {
@PoiExcel(value = "测试1",index = 1)
private String t1;
@PoiExcel(value = "测试2",index = 0)
private String t2;
public String getT1() {
return t1;
}
public void setT1(String t1) {
this.t1 = t1;
}
public String getT2() {
return t2;
}
public void setT2(String t2) {
this.t2 = t2;
}
}



