1. 程式人生 > 實用技巧 >EasyPOI解析Excel檔案

EasyPOI解析Excel檔案

之前寫過一篇匯出Excel的文章,時隔這麼長時間,再寫一篇解析吧

採用EasyPOI技術解析Excel,我感覺這個還是挺好用的,也可能是我沒有接觸過更好的技術了[捂臉]

匯入Maven依賴:

<!-- https://mvnrepository.com/artifact/cn.afterturn/easypoi-web -->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>3.0.3</version>
</dependency>

根據Excel定義模型(Model)

package model;

import cn.afterturn.easypoi.excel.annotation.Excel;

import java.io.Serializable;
import java.math.BigDecimal;

/**
    * 件
 * @author ZYGisComputer
 */
public class T0079J implements Serializable {
    /**
    * ID
    */
    private String f000Did;

    @Excel(name = "bh")
    
private String bh; private String f001Pid; private String f004OrgCode; private String f011Dasjkzbz; private String f012Titleinitials; private BigDecimal numofefile; @Excel(name = "swwz") private String swwz; @Excel(name = "ycdd") private String ycdd; @Excel(name
= "ycmc") private String ycmc; private String ycqm; @Excel(name = "ycrq") private String ycrq; @Excel(name = "ycsj") private String ycsj; @Excel(name = "zh") private String zh; @Excel(name = "zyyy") private String zyyy; public String getF000Did() { return f000Did; } }

根據Excel模板定義模型,這裡只演示最簡單的模型定義,也沒有增加校驗資訊,如果需要增加校驗資訊的可以,百度一下

定義解析工具類:

package utils;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import exception.TypeMismatchException;
import model.T0079J;

import java.io.File;
import java.util.List;

/**
 * ExcelUtil 工具類
 * @author ZYGisComputer
 */
public class ExcelUtil {

    private static final String DIAN = ".";

    private static final String XLS = "xls";

    private static final String XLSX = "xlsx";

    public static List<T0079J> parseExcel(File file,Class<?> clazz) throws TypeMismatchException {
        if(checkIsExcel(file)){
            return ExcelImportUtil.importExcel(file, clazz, new ImportParams());
        }
        throw new TypeMismatchException("檔案格式錯誤!");

    }

    public static boolean checkIsExcel(File file){
        if (null != file) {
            String fileName = file.getName();
            if(!fileName.contains(DIAN)){
                return false;
            }
            String type = fileName.substring(fileName.lastIndexOf(".") + 1);
            return XLS.equalsIgnoreCase(type) || XLSX.equalsIgnoreCase(type);
        }
        throw new NullPointerException("檔案為空");
    }

}

因為我的是檔案直接寫Main方法了

呼叫:

package executer;

import exception.TypeMismatchException;
import model.T0079J;
import org.apache.commons.lang3.StringUtils;
import utils.ExcelUtil;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author ZYGisComputer
 */
public class ImportT0097J {

    public static void main(String[] args) throws TypeMismatchException {


        File file = new File("C:\\File\\2020-11\\1.xls");

        List<T0079J> t0079JList = ExcelUtil.parseExcel(file, T0079J.class);

        for (T0079J x : t0079JList) {
            if (null!=x.getYcrq()) {
          // 格式化日期
                System.out.println(new SimpleDateFormat("yyyy/MM/dd").format(x.getYcrq()));
            }
            if (null!=x.getYcsj()) {
          // 格式化日期
                System.out.println(new SimpleDateFormat("HH:mm").format(x.getYcsj()));
            }
//            System.out.println(x);
        }
        System.out.println(1);

    }

}

因為之前沒有考慮日期的原因結果解析後的結果是直接的標準日期,處理起來比較費力

把模型中的ycrq和ycsj改為Date型別util包下的

這樣格式化日期比較簡單

到此解析成功

因為資料是涉密的,就不貼正式的Excel和解析Excel的截圖了

貼一個簡單的吧

上面的首行名稱對應 @Excel註解中的name值就可以了

比如這個圖來說就是

@Excel(name="id")

@Excel(name="姓名")

....

不需要解析的欄位不加@Excel就可以了

作者:彼岸舞

時間:2020\11\24

內容關於:POI

本文屬於作者原創,未經允許,禁止轉發