Search in sources :

Example 41 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project Hidoc by yuzhengyang.

the class TestController method export.

@GetMapping({ "export" })
public ResponseData export() {
    String fileName = DirTool.combine(R.Paths.Temp, UUIDTool.get() + ".xlsx");
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    // 如果这里想使用03 则 传入excelType参数即可
    ExcelWriter excelWriter = EasyExcel.write(fileName, SysAccessLog.class).build();
    WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").head(SysAccessLog.class).build();
    // region 分批处理
    for (int i = 0; i < 500; i++) {
        List<SysAccessLog> list = sysAccessLogMapper.selectList(null);
    // fillData(list);
    // excelWriter.write(list, writeSheet);
    }
    // endregion
    // //region 全量处理
    // List list = new ArrayList();
    // for (int i = 0; i < 500; i++) {
    // List<SysAccessLog> _l = sysAccessLogMapper.selectList(null);
    // list.addAll(_l);
    // }
    // fillData(list);
    // excelWriter.write(list, writeSheet);
    // //endregion
    excelWriter.finish();
    return ResponseData.ok();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) SysAccessLog(com.yuzhyn.hidoc.app.application.entity.sys.SysAccessLog)

Example 42 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project boot-admin by hb0730.

the class ExcelUtils method writeSheet.

/**
 * 多页签导出
 *
 * @param outputStream  out流
 * @param data          多页签数据key为sheet名称
 * @param excelTypeEnum excel类型
 * @param clazz         head
 * @param <T>           excel domain 泛型
 */
public static <T extends ExcelDomain> void writeSheet(@NonNull OutputStream outputStream, Map<String, List<T>> data, ExcelTypeEnum excelTypeEnum, Class<T> clazz) {
    excelTypeEnum = excelTypeEnum == null ? ExcelTypeEnum.XLS : excelTypeEnum;
    ExcelWriter writer = EasyExcel.write(outputStream, clazz).excelType(excelTypeEnum).build();
    if (!CollectionUtils.isEmpty(data)) {
        data.forEach((k, v) -> {
            WriteSheet sheet = EasyExcel.writerSheet(k).build();
            writer.write(v, sheet);
        });
    }
    writer.finish();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet)

Example 43 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project platform by elveahuang.

the class WriteTest method simpleWriteTest.

@Test
public void simpleWriteTest() {
    File file = new File("temp/simple.xlsx");
    ExcelWriter excelWriter = null;
    try {
        FileUtils.createParentDirectories(file);
        excelWriter = EasyExcel.write(file, UserDTO.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "模板1").build();
        excelWriter.write(Collections.EMPTY_LIST, writeSheet);
        writeSheet = EasyExcel.writerSheet(1, "模板2").build();
        excelWriter.write(Collections.EMPTY_LIST, writeSheet);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (excelWriter != null) {
            excelWriter.finish();
        }
    }
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 44 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project rebuild by getrebuild.

the class EasyExcelGenerator method generate.

/**
 * @return
 */
public File generate() {
    File tmp = RebuildConfiguration.getFileOfTemp(String.format("RBREPORT-%d.%s", System.currentTimeMillis(), template.getName().endsWith(".xlsx") ? "xlsx" : "xls"));
    List<Map<String, Object>> datas = buildData();
    // 无数据
    if (datas.isEmpty())
        return null;
    Map<String, Object> main = null;
    if (this.hasMain) {
        Iterator<Map<String, Object>> iter = datas.iterator();
        main = iter.next();
        iter.remove();
    }
    ExcelWriter excelWriter = null;
    FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
    try {
        excelWriter = EasyExcel.write(tmp).withTemplate(template).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(new FixsMergeStrategy()).build();
        // 有明细记录
        if (!datas.isEmpty()) {
            excelWriter.fill(datas, fillConfig, writeSheet);
        }
        // 主记录
        if (main != null) {
            excelWriter.fill(main, writeSheet);
        }
    } finally {
        if (excelWriter != null) {
            excelWriter.finish();
        }
    }
    return tmp;
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) File(java.io.File) FillConfig(com.alibaba.excel.write.metadata.fill.FillConfig)

Example 45 with ExcelWriter

use of com.alibaba.excel.ExcelWriter in project pancm_project by xuwujing.

the class EasyExcelWriteTest method excludeOrIncludeWrite.

public void excludeOrIncludeWrite() {
    // 注意 simpleWrite在数据量不大的情况下可以使用(5000以内,具体也要看实际情况),数据量大参照 重复多次写入
    // 写法1 JDK8+
    // since: 3.0.0-beta1
    String fileName = "/home" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    // 如果这里想使用03 则 传入excelType参数即可
    // EasyExcel.write(fileName, DemoData.class)
    // .sheet("模板")
    // .doWrite(
    // () -> {
    // // 分页查询数据
    // return data();
    // });
    // 写法2
    fileName = "/home" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    // 如果这里想使用03 则 传入excelType参数即可
    EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
    // 写法3
    fileName = "/home" + "/simpleWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里 需要指定写用哪个class去写
    ExcelWriter excelWriter = null;
    try {
        excelWriter = EasyExcel.write(fileName, DemoData.class).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
        excelWriter.write(data(), writeSheet);
    } finally {
        // 千万别忘记finish 会帮忙关闭流
        if (excelWriter != null) {
            excelWriter.finish();
        }
    }
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet)

Aggregations

ExcelWriter (com.alibaba.excel.ExcelWriter)67 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)52 Test (org.junit.Test)31 Sheet (com.alibaba.excel.metadata.Sheet)13 HashMap (java.util.HashMap)13 FillConfig (com.alibaba.excel.write.metadata.fill.FillConfig)11 List (java.util.List)9 FileOutputStream (java.io.FileOutputStream)7 IOException (java.io.IOException)7 WriteTable (com.alibaba.excel.write.metadata.WriteTable)6 ArrayList (java.util.ArrayList)6 ExcelReader (com.alibaba.excel.ExcelReader)5 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)5 FillWrapper (com.alibaba.excel.write.metadata.fill.FillWrapper)4 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)4 OutputStream (java.io.OutputStream)4 Map (java.util.Map)4 LargeDataTest (com.alibaba.easyexcel.test.core.large.LargeDataTest)3 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)3 WriteExcelException (com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException)3