poi操作Excel实现2003和2007版本兼容

数据格式转换工具类:

public class ExcelUtil {
    /**
     * 格式化单元格返回其内容 格式化成string返回。
     * 
     * @param cell
     * @return
     */
    public static String formatCell(Cell cell) {
        if (cell == null) {
            return "";
        } else {
            if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                return String.valueOf(cell.getBooleanCellValue());
            } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                return String.valueOf(cell.getNumericCellValue());
            } else {
                return String.valueOf(cell.getStringCellValue());
            }
        }
    }
    
    
    /**
     * 
     * 返回 日期  date  2019/3/28 14:18:00  这种类型的可以。
     */
    public static Date formatDate(Cell cell) throws ParseException{
        
        String str = cell.toString();
        
        Date date = null;
        if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            date  = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(str);
        } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            date = cell.getDateCellValue();
        }
        return date;
    }
    
    
    /**
     * 返回 BigDecimal 数据
     */
    public static BigDecimal formatBigDecimal(Cell cell) throws ParseException{
        String str = ExcelUtil.formatCell(cell);
        BigDecimal num = new BigDecimal(str.trim());
        return num;
    }
    
    
}

 

从Excel读取数据,返回存入数据的list集合:

public static List<Gross> grossUploadExcel(MultipartFile uploadFile)throws Exception{
        List<Gross> grosses = new ArrayList<Gross>();
        /*
         * File file = new File(pathUrl); InputStream inputStream = new
         * FileInputStream(file); POIFSFileSystem fs = new POIFSFileSystem(inputStream);
         */
        String filename = uploadFile.getOriginalFilename();
        // 1. 定义excel对象变量
        Workbook workbook = null;
        // 2. 判断后缀.决定使用的解析方式. 决定如何创建具体的对象
        if(filename.endsWith("xls")){
            System.out.println("执行的是xls");
            // Excel-2003
            workbook = new HSSFWorkbook(uploadFile.getInputStream());
        }else if(filename.endsWith("xlsx")){
            System.out.println("执行的是xlsx");
            // Excel-2007
            workbook = new XSSFWorkbook(uploadFile.getInputStream());
        }else{
            // 未知内容
            return null;
        }
        
        
        Sheet sheet = workbook.getSheetAt(0);//获取第一个sheet
        if (sheet!=null) {
            //遍历行ROW
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);//每一行
                if (row==null) {
                    continue;
                }
                //创建存放数据的对象
                Gross gross = new Gross();
                //遍历列cell
                for (int j = 0; j <= row.getLastCellNum(); j++) {
                    Cell cell = row.getCell(j);//每一列
                    if (cell==null) {
                        continue;
                    }
                    if (j==1) {
                        gross.setJingli(ExcelUtil.formatCell(cell));
                    }
                    if (j==2) {
                        gross.setDudao(ExcelUtil.formatCell(cell));
                    }
                    if (j==3) {
                        gross.setCantingId(ExcelUtil.formatCell(cell));
                    }
                    if (j==4) {
                        gross.setCantingName(ExcelUtil.formatCell(cell));
                    }
                    if (j==5) {
                    gross.setDate(ExcelUtil.formatDate(cell));
                    }
                    if (j==6) {
                        gross.setShouruType(ExcelUtil.formatCell(cell));
                    }
                    if (j==7) {
                        gross.setTc(ExcelUtil.formatBigDecimal(cell));
                    }
                    if (j==8) {
                        gross.setYingyee(ExcelUtil.formatBigDecimal(cell));
                    }
                    if (j==9) {
                        gross.setWuliaochengben(ExcelUtil.formatBigDecimal(cell));
                    }
                    if (j==10) {
                        gross.setMaoli(ExcelUtil.formatBigDecimal(cell));
                    }
                    if (j==11) {
                        gross.setMaolilv(ExcelUtil.formatBigDecimal(cell));
                    }
                    if (j==12) {
                        gross.setShangzhoutongbiyingyee(ExcelUtil.formatBigDecimal(cell));
                    }
                    if (j==13) {
                        gross.setShangzhoutongbimaoli(ExcelUtil.formatBigDecimal(cell));
                    }
                    if (j==14) {
                        gross.setShangzhoutongbimaolilv(ExcelUtil.formatBigDecimal(cell));
                    }
                }
                //把Excel数据添加进list集合里
                grosses.add(gross);
                workbook.close();
            }
            
        } else {
            System.out.println("sheet为空!");
        }
        return grosses;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值