Excel导入(POI)源码——模板导入+错误数据处理

Apache POI是种比较流行的API,它允许程序员使用Java程序创建修改和显示MS Office文件。这由Apache软件基金会开发使用Java分布式设计或修改Microsoft Office文件的开源库。它包含类和方法对用户输入数据或文件到MS Office文档进行解码。Apache的Jakata项目的POI子项目,目前比较成熟的是HSSF接口,处理MSExcel对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象,你可以控制一些属性如sheet,cell等等。

一.pom配置

<!-- 集中定义依赖版本号 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <poi.version>3.11</poi.version>
    </properties>

    <dependencies>
        <!-- Excel解析工具类 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>${poi.version}</version>
        </dependency>
    </dependencies>     

二.ExcelConstants.java静态常量

public class ExcelConstants {
   
    public static final String OFFICE_EXCEL_2003_POSTFIX = "xls"; // excel2003-2007文件后缀名
    public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx"; // excel2010文件后缀名
    public static final String EXCEL_DIR = "/excel"; // excel存储根目录
    public static final String EXCEL_USER_DIR = EXCEL_DIR + "/user"; // 用户的excel存储目录
    public static final String EXCEL_MODEL_DIR = EXCEL_DIR + "/model";// excel模板的存储目录
    public static final String EXCEL_USER_MODEL = "user_export_model.xlsx"; // 用户的excel模板名称
    public static final String INITIAL_PASSWORD = "000000"; // 用户导入时的初始密码
    public static final int START_SHEET = 0; // excel文件开始读取的sheet数
    public static final int START_ROW = 1; // excel文件开始读取的行数
    public static final String ERROR_MESSAGE = "errorMessage";// 导入时的错误信息
    public static final String ERROR_COUNT = "errorCount";// 失败条数
    public static final String SUCCESS_COUNT = "successCount";// 成功条数
    public static final String ERROR_POSTFIX = ".xlsx";// 错误信息文件后缀名
    public static final String ERROR_SEPARATOR = ";"; // 错误信息分隔符
    public static final String USER = "user"; // 导入user集合的key

    public static final String role_expert = "专家";  // 专家
    public static final String role_trainee = "学员"; // 学员
    public static final String gender_man = "男";  // 男
    public static final String gender_woman = "女"; // 女
}

三.ErrorMessage.java错误数据实体

public class ErrorMessage {
    // 错误信息所在工作表
    private Integer sheetNum;
    // 错误信息所在的具体行数
    private Integer rowNum;
    // 错误信息提示
    private String message;

    public Integer getSheetNum() {
        return sheetNum;
    }

    public void setSheetNum(Integer sheetNum) {
        this.sheetNum = sheetNum;
    }

    public Integer getRowNum() {
        return rowNum;
    }

    public void setRowNum(Integer rowNum) {
        this.rowNum = rowNum;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

四.ReadExcel.java 表格处理类

/**
 * 
 * <p>Title: ReadExcel</p> 
 * <p>Description: </p>
 * @author xy
 * @date 2018年6月27日 
 */
public class ReadExcel {
   

    /**
     * 选择处理Excel方式
     * 
     * <p>Title: readImportExcel</p> 
     * <p>Description: </br>获取导入的Excel表的后缀(xls或xlsx)
     *                 </br>然后根据不同的格式执行不同的处理Excel方法</p>  
     * @time 上午11:39:18 
     * @param path --->exc
  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值