use of com.hummer.excel.plugin.model.write.TableWriteDataBo in project hummer-framework by hummer-team.
the class TableWriteTemplate method tableWrite.
@Override
public void tableWrite(List<TableWriteDataBo> list, String sheetName, OutputStream os) {
AppBusinessAssert.isTrue(CollectionUtils.isNotEmpty(list), 400, "export excel by table type " + ",list is empty");
// 这里直接写多个table的案例了,如果只有一个 也可以直一行代码搞定,参照其他案例
// 这里 需要指定写用哪个class去写
com.alibaba.excel.ExcelWriter excelWriter = null;
try {
excelWriter = EasyExcel.write(os).build();
// 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).needHead(Boolean.FALSE).build();
// 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
for (int i = 0; i < list.size(); i++) {
TableWriteDataBo item = list.get(i);
WriteTable writeTable = EasyExcel.writerTable(i).needHead(item.isWithHead()).head(item.getHeadClass()).build();
// 写入会创建头
excelWriter.write(item.getDataInfos(), writeSheet, writeTable);
}
} finally {
// 千万别忘记finish 会帮忙关闭流
if (excelWriter != null && os != null) {
try {
excelWriter.finish();
} catch (Exception e) {
LOGGER.debug("excelWriter finish fail,{}", e.getMessage());
}
}
}
}
use of com.hummer.excel.plugin.model.write.TableWriteDataBo in project hummer-framework by hummer-team.
the class ReaderTest method testTableExcel.
@Test
public void testTableExcel() {
List<TableWriteDataBo> list = new ArrayList<>(2);
TableWriteDataBo bo = new TableWriteDataBo();
bo.setHeadClass(HeaderDemo.class);
bo.setDataInfos(data(1, HeaderDemo.class));
TableWriteDataBo bo2 = new TableWriteDataBo();
bo2.setHeadClass(HeaderDemo2.class);
bo2.setDataInfos(data(5, HeaderDemo2.class));
list.add(bo);
list.add(bo2);
String sheet = "模板";
String fileName = TestFileUtil.getPath() + "tableWrite" + System.currentTimeMillis() + ".xlsx";
File file = new File(fileName);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
TableWriteTemplate.getInstance().tableWrite(list, sheet, outputStream);
}
use of com.hummer.excel.plugin.model.write.TableWriteDataBo in project hummer-framework by hummer-team.
the class ExcelController method queryUserContext.
@ApiOperation("多标题栏导出")
@GetMapping("/export/table")
public void queryUserContext(HttpServletRequest request, HttpServletResponse response) throws IOException {
List<TableWriteDataBo> list = new ArrayList<>(2);
TableWriteDataBo bo = new TableWriteDataBo();
bo.setHeadClass(HeaderDemo.class);
bo.setDataInfos(data(1, HeaderDemo.class));
TableWriteDataBo bo2 = new TableWriteDataBo();
bo2.setHeadClass(HeaderDemo2.class);
bo2.setDataInfos(data(5, HeaderDemo2.class));
bo2.setWithHead(true);
list.add(bo);
list.add(bo2);
String sheet = "table";
String fileName = "tableWrite_" + System.currentTimeMillis() + ExcelTypeEnum.XLSX.getValue();
ServletOutputStream os = response.getOutputStream();
HttpServletResponseUtil.composeExcelResponseHeaders(response, fileName);
TableWriteTemplate.getInstance().tableWrite(list, sheet, os);
}
Aggregations