Search in sources :

Example 1 with TableWriteDataBo

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());
            }
        }
    }
}
Also used : WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) WriteTable(com.alibaba.excel.write.metadata.WriteTable) TableWriteDataBo(com.hummer.excel.plugin.model.write.TableWriteDataBo)

Example 2 with TableWriteDataBo

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);
}
Also used : FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) HeaderDemo(com.hummer.excel.plugin.test.model.HeaderDemo) HeaderDemo2(com.hummer.excel.plugin.test.model.HeaderDemo2) File(java.io.File) TableWriteDataBo(com.hummer.excel.plugin.model.write.TableWriteDataBo) Test(org.junit.Test)

Example 3 with TableWriteDataBo

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);
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ArrayList(java.util.ArrayList) HeaderDemo(com.hummer.nacos.model.HeaderDemo) HeaderDemo2(com.hummer.nacos.model.HeaderDemo2) TableWriteDataBo(com.hummer.excel.plugin.model.write.TableWriteDataBo) GetMapping(org.springframework.web.bind.annotation.GetMapping) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

TableWriteDataBo (com.hummer.excel.plugin.model.write.TableWriteDataBo)3 ArrayList (java.util.ArrayList)2 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)1 WriteTable (com.alibaba.excel.write.metadata.WriteTable)1 HeaderDemo (com.hummer.excel.plugin.test.model.HeaderDemo)1 HeaderDemo2 (com.hummer.excel.plugin.test.model.HeaderDemo2)1 HeaderDemo (com.hummer.nacos.model.HeaderDemo)1 HeaderDemo2 (com.hummer.nacos.model.HeaderDemo2)1 ApiOperation (io.swagger.annotations.ApiOperation)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Test (org.junit.Test)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1