栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

PoiExcel数据导入工具类(持续更新)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PoiExcel数据导入工具类(持续更新)

1、引入poi 1)poi版本
4.1.0
2)pom.xml

    org.apache.poi
    poi-ooxml
    ${poi.version}


    org.apache.poi
    poi-ooxml-schemas
    ${poi.version}


    org.apache.poi
    poi
    ${poi.version}

2、PoiExcel.java 完整代码
import cn.jjzqkj.base.defaultEnum.RegType;
import cn.jjzqkj.base.exception.BusinessException;
import cn.jjzqkj.base.util.RegExpLib;
import cn.jjzqkj.base.util.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


public class PoiExcel {

    private final Workbook workBook;

    public PoiExcel(String filePath) throws IOException {

        this(new File(filePath));
    }

    public PoiExcel(File file) throws IOException {

        this.verifyXls(file);

        InputStream inputStream = new FileInputStream(file);

        // 根据不同的后缀,创建不同的对象
        if(StringUtil.isIncludeStr(file.getName(), ".*\.xlsx$", RegType.CASE_INSENSITIVE)){

            this.workBook = new XSSFWorkbook(inputStream);
        } else{

            this.workBook = new HSSFWorkbook(inputStream);
        }
    }

    
    public Object getObjVal(Integer sheetIndex, Integer rowIndex, Integer cellIndex) {

        Cell cell = this.getSheet(sheetIndex).getRow(rowIndex).getCell(cellIndex);
        switch (cell.getCellType()) {
            case STRING: return cell.getStringCellValue();
            case NUMERIC: return cell.getNumericCellValue();
            case BOOLEAN: return cell.getBooleanCellValue();
            default: return null;
        }

    }

    
    public String getStringVal(Integer sheetIndex, Integer rowIndex, Integer cellIndex) {

        Object cellVal = this.getObjVal(sheetIndex, rowIndex, cellIndex);
        return null != cellVal?cellVal.toString(): "";
    }

    
    public Double getDoubleval(Integer sheetIndex, Integer rowIndex, Integer cellIndex) {

        Object cellVal = this.getObjVal(sheetIndex, rowIndex, cellIndex);
        return null == cellVal?.0: StringUtil.isIncludeStr(cellVal.toString(), RegExpLib.REG_FLOAT, RegType.DOTALL)?Double.parseDouble(cellVal.toString()): .0;
    }

    
    public boolean getBooleanVal(Integer sheetIndex, Integer rowIndex, Integer cellIndex) {

        Object cellVal = this.getObjVal(sheetIndex, rowIndex, cellIndex);
        return null != cellVal && (boolean) cellVal;
    }

    public Sheet getSheet(Integer sheetIndex) {

        return this.workBook.getSheetAt(sheetIndex);
    }

    private void verifyXls(File file){

        if(!file.exists() || file.isDirectory()) {

            throw new BusinessException("导入文件不存在!");
        }

        if(!StringUtil.isIncludeStr(file.getName(), ".*\.xls$|.*\.xlsx$", RegType.CASE_INSENSITIVE)) {

            throw new BusinessException("导入文件类型错误!");
        }
    }

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

        PoiExcel excel = new PoiExcel("F:\Users\ZhangJi\yx-tech\project\鲜花商城\设计文档\价格动态导入模板.xlsx");

        int rowIndex = 1;
        while (true) {

            int cellIndex = 0;
            String v1 = excel.getStringVal(0, rowIndex, cellIndex++);
            if(StringUtil.isEmpty(v1)) {

                break;
            }

            String v2 = excel.getStringVal(0, rowIndex, cellIndex++);
            double v3 = excel.getDoubleval(0, rowIndex, cellIndex++);
            double v4 = excel.getDoubleval(0, rowIndex, cellIndex++);
            double v5 = excel.getDoubleval(0, rowIndex, cellIndex);

            System.out.println(v1 + " - " + v2 + " - " + v3 + " - " + v4 + " - " + v5);
            rowIndex++;
        }
    }
}

3、相关参数、方法 1)字符串判空
	public static boolean isEmpty(String str, boolean isExistSpace) {
		if(null == str)	{
			return true;
		}
		if("".equals(str) || str.length() == 0)	{
			return true;
		}
		return !isExistSpace && "".equals(str.trim());
	}
2)字符串验证
	public static boolean isIncludeStr(String str, String reg, RegType type) {
		if(isEmpty(str)) {
			return false;
		}

		return regStr(type, reg, str).find();
	}
3)字符串匹配
	public static Matcher regStr(RegType type, String reg, String str) {
		Pattern pat = Pattern.compile(reg, type.getValue());

		return pat.matcher(str);
	}
4)字符串匹配验证类型枚举类
import java.util.regex.Pattern;


public enum RegType {
        
        DOTALL(1, Pattern.DOTALL),

        
        CASE_INSENSITIVE(2, Pattern.CASE_INSENSITIVE);

        private final int value;
        private final int type;

        RegType(int value, int type) {
            this.value = value;
            this.type = type;
        }

        public int getValue() {
            return value;
        }

        public int getType() {
            return type;
        }
    }
5)实数正则表达式
    public final static String REG_FLOAT = "^[\+|\-|[1-9]|0]{1}\d*[.]{0,1}\d*[1-9|0]{1}$";
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/684971.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号