Search in sources :

Example 1 with WriteTable

use of com.alibaba.excel.write.metadata.WriteTable in project easyexcel by alibaba.

the class WriteTest method tableWrite.

/**
 * 使用table去写入
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link DemoData}
 * <p>
 * 2. 然后写入table即可
 */
@Test
public void tableWrite() {
    String fileName = TestFileUtil.getPath() + "tableWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里 需要指定写用哪个class去写
    try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()) {
        // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
        WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build();
        // 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
        WriteTable writeTable0 = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build();
        WriteTable writeTable1 = EasyExcel.writerTable(1).needHead(Boolean.TRUE).build();
        // 第一次写入会创建头
        excelWriter.write(data(), writeSheet, writeTable0);
        // 第二次写如也会创建头,然后在第一次的后面写入数据
        excelWriter.write(data(), writeSheet, writeTable1);
    }
    // 方法2 这里直接写多个table的案例了,如果只有一个 也可以直一行代码搞定,参照其他案, 不使用 try-with-resources
    // 这里 需要指定写用哪个class去写
    ExcelWriter excelWriter = null;
    try {
        excelWriter = EasyExcel.write(fileName, DemoData.class).build();
        // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
        WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build();
        // 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
        WriteTable writeTable0 = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build();
        WriteTable writeTable1 = EasyExcel.writerTable(1).needHead(Boolean.TRUE).build();
        // 第一次写入会创建头
        excelWriter.write(data(), writeSheet, writeTable0);
        // 第二次写如也会创建头,然后在第一次的后面写入数据
        excelWriter.write(data(), writeSheet, writeTable1);
    } finally {
        // 千万别忘记close 会帮忙关闭流
        if (excelWriter != null) {
            excelWriter.close();
        }
    }
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) WriteTable(com.alibaba.excel.write.metadata.WriteTable) Test(org.junit.Test)

Example 2 with WriteTable

use of com.alibaba.excel.write.metadata.WriteTable in project easyexcel by alibaba.

the class RepetitionDataTest method readAndWriteTable.

private void readAndWriteTable(File file) {
    try (ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build()) {
        WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
        WriteTable writeTable = EasyExcel.writerTable(0).relativeHeadRowIndex(0).build();
        excelWriter.write(data(), writeSheet, writeTable).write(data(), writeSheet, writeTable);
    }
    try (ExcelReader excelReader = EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener()).build()) {
        ReadSheet readSheet = EasyExcel.readSheet(0).headRowNumber(2).build();
        excelReader.read(readSheet);
    }
}
Also used : ExcelReader(com.alibaba.excel.ExcelReader) ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) WriteTable(com.alibaba.excel.write.metadata.WriteTable) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet)

Example 3 with WriteTable

use of com.alibaba.excel.write.metadata.WriteTable 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 4 with WriteTable

use of com.alibaba.excel.write.metadata.WriteTable in project study by bage2014.

the class WriteTest method tableWrite.

/**
 * 使用table去写入
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link DemoData}
 * <p>
 * 2. 然后写入table即可
 */
@Test
public void tableWrite() {
    String fileName = TestFileUtil.getPath() + "tableWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里直接写多个table的案例了,如果只有一个 也可以直一行代码搞定,参照其他案例
    // 这里 需要指定写用哪个class去写
    ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build();
    // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
    WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build();
    // 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
    WriteTable writeTable0 = EasyExcel.writerTable(0).needHead(Boolean.TRUE).build();
    WriteTable writeTable1 = EasyExcel.writerTable(1).needHead(Boolean.TRUE).build();
    // 第一次写入会创建头
    excelWriter.write(data(), writeSheet, writeTable0);
    // 第二次写如也会创建头,然后在第一次的后面写入数据
    excelWriter.write(data(), writeSheet, writeTable1);
    // / 千万别忘记finish 会帮忙关闭流
    excelWriter.finish();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) WriteTable(com.alibaba.excel.write.metadata.WriteTable) Test(org.junit.Test)

Example 5 with WriteTable

use of com.alibaba.excel.write.metadata.WriteTable in project easyexcel by alibaba.

the class Wirte method tableWrite.

@Test
public void tableWrite() {
    String fileName = TestFileUtil.getPath() + "tableWrite" + System.currentTimeMillis() + ".xlsx";
    // 这里直接写多个table的案例了,如果只有一个 也可以直一行代码搞定,参照其他案例
    // 这里 需要指定写用哪个class去写
    ExcelWriter excelWriter = EasyExcel.write(fileName).build();
    // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
    WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
    // 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
    WriteTable writeTable0 = EasyExcel.writerTable(0).head(DemoData1.class).build();
    // 第一次写入会创建头
    excelWriter.write(data(), writeSheet, writeTable0);
    // 第二次写如也会创建头,然后在第一次的后面写入数据
    // / 千万别忘记close 会帮忙关闭流
    excelWriter.finish();
}
Also used : ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) WriteTable(com.alibaba.excel.write.metadata.WriteTable) Test(org.junit.Test)

Aggregations

WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)7 WriteTable (com.alibaba.excel.write.metadata.WriteTable)7 ExcelWriter (com.alibaba.excel.ExcelWriter)6 ExcelReader (com.alibaba.excel.ExcelReader)3 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)3 Test (org.junit.Test)3 MapCache (com.alibaba.excel.cache.MapCache)2 FileOutputStream (java.io.FileOutputStream)2 StringStringConverter (com.alibaba.excel.converters.string.StringStringConverter)1 TableWriteDataBo (com.hummer.excel.plugin.model.write.TableWriteDataBo)1