springboot項(xiàng)目整合easypoi實(shí)現(xiàn)導(dǎo)入導(dǎo)出功能

導(dǎo)入依賴

在導(dǎo)入依賴錢看是否項(xiàng)目中存在poi相關(guān)的版本。直接從pom文件中去除exclusion。

本次使用的是4.2.0 。maven依賴如下:

<dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.2.0</version>

        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.1.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.google.guava</groupId>
                    <artifactId>guava</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

貌似沒(méi)用到easypoi-base 。先不管了

按照官方說(shuō)明。創(chuàng)建實(shí)體

官方文檔;

public class MaterialExport implements Serializable {
// 物資名稱
    @Excel(name = "物資名稱", width = 15)
    private String name;
    // 物資規(guī)格
    @Excel(name = "物資規(guī)格")
    private String specs;
    // 物資類型
    @Excel(name = "物資類型")
    private String type;

    // 單位
    @Excel(name = "單位")
    private String unit;
    // 庫(kù)存
    @Excel(name = "庫(kù)存")
    private long num;
    
    //單價(jià)
    @Excel(name = "單價(jià)")
    private BigDecimal unitprice;
    // 廠家
    @Excel(name = "廠家", width = 20)
    private String manufactor;
    // 庫(kù)存位置
    @Excel(name = "庫(kù)存位置", width = 20)
    private String address;
    // 用途
    @Excel(name = "用途", width = 20)
    private String purpose;

    @Excel(name = "入庫(kù)時(shí)間",format = "yyyy年MM月dd日", width = 25)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    // 備注
    @Excel(name = "備注", width = 40)
    private String remarks;
    //getter setter方法省略。

}

在導(dǎo)入時(shí)需要注意:實(shí)體中用到的屬性為java基本屬性或者是在“java.lang”、“java.math”、“java.sql”、“java.util”包中具體easypoi中邏輯代碼為:

/**
     * 是不是java基礎(chǔ)類
     *
     * @param field
     * @return
     */
    public static boolean isJavaClass(Field field) {
        Class<?> fieldType = field.getType();
        boolean isBaseClass = false;
        if (fieldType.isArray()) {
            isBaseClass = false;
        } else if (fieldType.isPrimitive() || fieldType.getPackage() == null
                   || "java.lang".equals(fieldType.getPackage().getName())
                   || "java.math".equals(fieldType.getPackage().getName())
                   || "java.sql".equals(fieldType.getPackage().getName())
                   || "java.util".equals(fieldType.getPackage().getName())) {
            isBaseClass = true;
        }
        return isBaseClass;
    }

導(dǎo)出功能

獲取數(shù)據(jù)后直接調(diào)用Util方法
controller:

public Result export(HttpServletRequest request, HttpServletResponse response) {
        System.out.println(sdf.format(new Date()));
        //此處為從數(shù)據(jù)庫(kù)獲取list
         List<MaterialExport> materialExports = materialService.export();
        response.setContentType("application/vnd.ms-excel");
        OutputStream out = null;
        try {
            response.setHeader("Content-disposition",
                    "attachment;filename=" + URLEncoder.encode("物資導(dǎo)出" + sdf.format(new Date()) + ".xls","ISO-8859-1" ));
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("物資庫(kù)存信息", "物資"),

                    MaterialExport.class, materialExports);
            out = response.getOutputStream();
            workbook.write(out);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e);
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }

        return Response.success();
    }

導(dǎo)入

從文件流中獲取數(shù)據(jù)用easypoi解析數(shù)據(jù):

    public Result inport(HttpServletRequest request,@RequestParam("file") MultipartFile fileItem) {
    ImportParams importParams = new ImportParams();
      
        importParams.setHeadRows(2);
        // 獲取模板
        ExcelImportResult<MaterialExport> data = null;
        try {
            data = ExcelImportUtil.importExcelMore(fileItem.getInputStream(), MaterialExport.class, importParams);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 獲取模板數(shù)據(jù)
        List<MaterialExport> successList = data.getList();
        int total = successList.size();
        // 循環(huán)數(shù)據(jù)
        System.out.println(total);
        for (MaterialExport excelDTO : successList) {
            System.out.println(excelDTO.toString());
        }
        return Response.success();
    }

完畢。

本文由博客群發(fā)一文多發(fā)等運(yùn)營(yíng)工具平臺(tái) OpenWrite 發(fā)布

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容