java 读取excel

public class ExcelPOI {

    /**
     * 解析excel
     * @param filePath 文件路径
     * @param index 从第n行开始读取数据
     * @param str 指定字符串,在读取到该字符是停止读取(必须保证该字符串在excel每页的单元格中是唯一的)
     */
    public String POIReadExcel(String filePath, int index, String str) throws IOException, IllegalAccessException, InstantiationException {
        FileInputStream fs=new FileInputStream(filePath);
        long count=fs.getChannel().size();
        //判断excel内容是否为空
        if(count<=0){
            return null;
        }
        HSSFWorkbook wb=new HSSFWorkbook(fs);
        for (int k = 0; k < wb.getNumberOfSheets(); k++){
            HSSFSheet sheet = wb.getSheetAt(k);
            //总行数
            int rows = sheet.getPhysicalNumberOfRows();
            int r=index;
            boolean flag=true;
            for (; r < rows; r++){
                HSSFRow row = sheet.getRow(r);
                //判断当前行是否为空
                if (isRowEmpty(row)) {
                    continue;
                }
                //总列数
                int cells = row.getPhysicalNumberOfCells();
                //拼接字符串
                String data="";
                for (int c = 0; c < cells; c++){
                    HSSFCell cell = row.getCell(c);
                    //转换日期格式数据
                    String value=importByExcelForDate(cell);
                    if(null==value){
                        switch (cell.getCellType()){
                            case HSSFCell.CELL_TYPE_FORMULA:
                                value = cell.getCellFormula();
                                break;
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                value= ""+ cell.getNumericCellValue();
                                break;
                            case HSSFCell.CELL_TYPE_STRING:
                                value = cell.getStringCellValue();
                                break;
                            default:
                        }
                        //判断每一列的值是否为空
                        if(null==value){
                            value="NULL";
                        }
                        //判断当前列的值是否为指定字符,如果是跳出循环
                        if(null!=str && str.length()>0){
                            if(str.equals(value)){
                                flag=false;
                                break;
                            }
                        }
                    }
                    data+="|"+value;
                }
                if(!flag){//跳出循环
                    break;
                }
                data=data.substring(1,data.length());
            }
        }
        return data;
    }
    /**
     * 处理POI导入Excel中日期格式数据混乱问题
     * @param currentCell
     * @return currentCellValue
     */
    public static String importByExcelForDate(Cell currentCell) {
        String currentCellValue = null;
        try {
            if (DateUtil.isCellDateFormatted(currentCell)) {
                // 用于转化为日期格式
                Date d = currentCell.getDateCellValue();
                DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
                currentCellValue = formater.format(d);
            }
        } catch (Exception e) {
            return currentCellValue;
        }
        return currentCellValue;
    }

    /**
     * 判断当前行是否为空
     * @param row
     * @return
     */
    public static boolean isRowEmpty(HSSFRow row){
        if(null==row){
            return true;
        }
        for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
            Cell cell = row.getCell(i);
            if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK){
                return false;
            }
        }
        return true;
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值