当前位置:   article > 正文

Easyexcel数据追加模式_easyexcel在原有内容上添加数据

easyexcel在原有内容上添加数据

引入依赖

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.9.0</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.1.1</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.20</version>
</dependency>  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

demo案例

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.List;


public class TestEasyExcelAppend {

    public static void main(String[] args) throws IOException {
        File destFile = new File("D:\\data\\excel\\workbook-result.xls");

        if (destFile.exists()) {
            FileUtils.forceDelete(destFile);
        }
        for (int i = 0; i < 100; i++) {
            writeExcel(destFile, DemoData.class, data());
        }
    }

    private static void writeExcel(File destFile, Class type, Collection<?> data) throws IOException {
        FileUtils.forceMkdirParent(destFile);
        ExcelWriter excelWriter = null;
        if (destFile.exists()) {
            File tempFile = new File(destFile.getParent(), "workbook-temp.xls");
            if (tempFile.exists()){
                FileUtils.forceDelete(tempFile);
            }
            FileUtils.copyFile(destFile,tempFile);
            //追加数据,目标文件与原始文件不能是同一个文件名  withTemplate()指定模板文件
            excelWriter = EasyExcel.write().withTemplate(tempFile).excelType(ExcelTypeEnum.XLSX)
                    //.file() 指定目标文件,不能与模板文件是同一个文件
                    .file(destFile).autoCloseStream(false).build();
            doWrite(excelWriter, data);
            if (tempFile.exists()) {
                FileUtils.forceDelete(tempFile);
            }
        } else {
            excelWriter = EasyExcel.write(destFile, type).excelType(ExcelTypeEnum.XLSX)
                    .build();
            doWrite(excelWriter, data);
        }
    }

    private static void doWrite(ExcelWriter excelWriter, Collection<?> data) {
        try {
            WriteSheet writeSheet = EasyExcel.writerSheet("人员清单")
                    .build();
            excelWriter.write(data, writeSheet);
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (null != excelWriter) {
                excelWriter.finish();
            }
        }
    }

    private static List<DemoData> data() {
        List<DemoData> list = ListUtils.newArrayList();
        for (int i = 0; i < 10000; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }
    /**
     * 基础数据类
     *
     * @author Jiaju Zhuang
     **/
    @Getter
    @Setter
    @EqualsAndHashCode
    public static class DemoData {
        @ExcelProperty("字符串标题")
        private String string;
        @ExcelProperty("日期标题")
        private Date date;
        @ExcelProperty("数字标题")
        private Double doubleData;
        /**
         * 忽略这个字段
         */
        @ExcelIgnore
        private String ignore;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/459388
推荐阅读
相关标签
  

闽ICP备14008679号