定义ExcelViewObject
public class ExcelViewObject {
@ColumnWidth(12)
@ExcelProperty(value = "首列")
private String type;
@ColumnWidth(12)
@ExcelProperty(value = "第二列")
private String name;
}
插入数据,输出到ByteArrayOutputStream;ByteArrayOutputStream作为常用的字节流,输出后可以作为接口数据来传送;
ByteArrayOutputStream out = new ByteArrayOutputStream();
List<ExcelViewObject> data = new ArrayList<>();
将要插入的数据遍历插入,如果不需转换直接addAll();
for (POObject item : list) {
ExcelViewObject excelViewObject = new ExcelViewObject();
BeanUtils.copyProperties(item, excelViewObject);
data.add(ExcelViewObject);
}
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.RIGHT);
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
EasyExcel.write(out, ExcelViewObject.class)
.registerWriteHandler(horizontalCellStyleStrategy).sheet().doWrite(data);