Search in sources :

Example 1 with LoopMergeStrategy

use of com.alibaba.excel.write.merge.LoopMergeStrategy in project easyexcel by alibaba.

the class WriteTest method mergeWrite.

/**
 * 合并单元格
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link DemoData} {@link DemoMergeData}
 * <p>
 * 2. 创建一个merge策略 并注册
 * <p>
 * 3. 直接写即可
 *
 * @since 2.2.0-beta1
 */
@Test
public void mergeWrite() {
    // 方法1 注解
    String fileName = TestFileUtil.getPath() + "mergeWrite" + System.currentTimeMillis() + ".xlsx";
    // 在DemoStyleData里面加上ContentLoopMerge注解
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    EasyExcel.write(fileName, DemoMergeData.class).sheet("模板").doWrite(data());
    // 方法2 自定义合并单元格策略
    fileName = TestFileUtil.getPath() + "mergeWrite" + System.currentTimeMillis() + ".xlsx";
    // 每隔2行会合并 把eachColumn 设置成 3 也就是我们数据的长度,所以就第一列会合并。当然其他合并策略也可以自己写
    LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0);
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    EasyExcel.write(fileName, DemoData.class).registerWriteHandler(loopMergeStrategy).sheet("模板").doWrite(data());
}
Also used : LoopMergeStrategy(com.alibaba.excel.write.merge.LoopMergeStrategy) Test(org.junit.Test)

Example 2 with LoopMergeStrategy

use of com.alibaba.excel.write.merge.LoopMergeStrategy in project java-example by saxingz.

the class EasyExcelTest method main.

public static void main(String[] args) {
    // 方法1 注解
    String fileName = TestFileUtil.getPath() + "mergeWrite" + System.currentTimeMillis() + ".xlsx";
    // 在DemoStyleData里面加上ContentLoopMerge注解
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    // EasyExcel.write(fileName, DemoMergeData.class).sheet("模板").doWrite(data());
    // 方法2 自定义合并单元格策略
    fileName = TestFileUtil.getPath() + "mergeWrite" + System.currentTimeMillis() + ".xlsx";
    // 每隔2行会合并 把eachColumn 设置成 3 也就是我们数据的长度,所以就第一列会合并。当然其他合并策略也可以自己写
    LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0);
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// EasyExcel.write(fileName, DemoData.class).registerWriteHandler(loopMergeStrategy).sheet("模板").doWrite(data());
}
Also used : LoopMergeStrategy(com.alibaba.excel.write.merge.LoopMergeStrategy)

Example 3 with LoopMergeStrategy

use of com.alibaba.excel.write.merge.LoopMergeStrategy in project study by bage2014.

the class WriteTest method mergeWrite.

/**
 * 合并单元格
 * <p>
 * 1. 创建excel对应的实体对象 参照{@link DemoData}
 * <p>
 * 2. 创建一个merge策略 并注册
 * <p>
 * 3. 直接写即可
 */
@Test
public void mergeWrite() {
    String fileName = TestFileUtil.getPath() + "mergeWrite" + System.currentTimeMillis() + ".xlsx";
    // 每隔2行会合并 把eachColumn 设置成 3 也就是我们数据的长度,所以就第一列会合并。当然其他合并策略也可以自己写
    LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0);
    // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
    EasyExcel.write(fileName, DemoData.class).registerWriteHandler(loopMergeStrategy).sheet("模板").doWrite(data());
}
Also used : LoopMergeStrategy(com.alibaba.excel.write.merge.LoopMergeStrategy) Test(org.junit.Test)

Example 4 with LoopMergeStrategy

use of com.alibaba.excel.write.merge.LoopMergeStrategy in project easyexcel by alibaba.

the class AbstractWriteHolder method dealLoopMerge.

private void dealLoopMerge(List<WriteHandler> handlerList, Head head) {
    LoopMergeProperty loopMergeProperty = head.getLoopMergeProperty();
    if (loopMergeProperty == null) {
        return;
    }
    handlerList.add(new LoopMergeStrategy(loopMergeProperty, head.getColumnIndex()));
}
Also used : LoopMergeStrategy(com.alibaba.excel.write.merge.LoopMergeStrategy) LoopMergeProperty(com.alibaba.excel.metadata.property.LoopMergeProperty)

Example 5 with LoopMergeStrategy

use of com.alibaba.excel.write.merge.LoopMergeStrategy in project easyexcel by alibaba.

the class FillDataTest method complexFill.

private void complexFill(File file, File template) {
    try (ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build()) {
        WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(new LoopMergeStrategy(2, 0)).build();
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        excelWriter.fill(data(), fillConfig, writeSheet);
        excelWriter.fill(data(), fillConfig, writeSheet);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("date", "2019年10月9日13:28:28");
        map.put("total", 1000);
        excelWriter.fill(map, writeSheet);
    }
    List<Object> list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync();
    Assert.assertEquals(list.size(), 21L);
    Map<String, String> map19 = (Map<String, String>) list.get(19);
    Assert.assertEquals("张三", map19.get(0));
}
Also used : LoopMergeStrategy(com.alibaba.excel.write.merge.LoopMergeStrategy) HashMap(java.util.HashMap) ExcelWriter(com.alibaba.excel.ExcelWriter) WriteSheet(com.alibaba.excel.write.metadata.WriteSheet) HashMap(java.util.HashMap) Map(java.util.Map) FillConfig(com.alibaba.excel.write.metadata.fill.FillConfig)

Aggregations

LoopMergeStrategy (com.alibaba.excel.write.merge.LoopMergeStrategy)5 Test (org.junit.Test)2 ExcelWriter (com.alibaba.excel.ExcelWriter)1 LoopMergeProperty (com.alibaba.excel.metadata.property.LoopMergeProperty)1 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)1 FillConfig (com.alibaba.excel.write.metadata.fill.FillConfig)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1