poi解析Excel

来源:互联网 发布:淘宝运费险能赔付多少 编辑:程序博客网 时间:2024/05/21 11:28
/**     * 对外提供读取excel 的方法     * */    public static List<List<Object>> readExcel(File file) throws IOException {        String fileName = file.getName();        String extension = fileName.lastIndexOf(".") == -1 ? "" : fileName                .substring(fileName.lastIndexOf(".") + 1);        if ("xls".equals(extension)) {            return read2003Excel(file);        } else if ("xlsx".equals(extension)) {            return read2007Excel(file);        } else {            throw new IOException("不支持的文件类型");        }    }    /**     * 读取 office 2003 excel     *     * @throws IOException     * @throws FileNotFoundException     */    private static List<List<Object>> read2003Excel(File file) {        List<List<Object>> list = new LinkedList<List<Object>>();        try{        HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));        HSSFFormulaEvaluator  evaluator = new HSSFFormulaEvaluator(hwb);        //HSSFSheet sheet = null;        HSSFSheet sheet = hwb.getSheetAt(0);        Object value = null;        HSSFRow row = null;        HSSFCell cell = null;        int counter = 0;//        for (int m = 0; m< hwb.getNumberOfSheets() ;m++){//            sheet=hwb.getSheetAt(m);//            if(sheet == null){//                continue;//            }        for (int i = sheet.getFirstRowNum(); counter < sheet                .getPhysicalNumberOfRows(); i++) {            row = sheet.getRow(i);            if (row == null) {                continue;            } else {                counter++;            }            List<Object> linked = new LinkedList<Object>();//            for (int j = row.getFirstCellNum(); j <= 17; j++) {                for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {                cell = row.getCell(j);                if (cell == null) {                    continue;                }                DecimalFormat df = new DecimalFormat("0");// 格式化 number String                                                            // 字符                SimpleDateFormat sdf = new SimpleDateFormat(                        "yyyy-MM-dd");// 格式化日期字符串                switch (cell.getCellType()) {                case XSSFCell.CELL_TYPE_STRING:                    value = cell.getStringCellValue();                    break;                case XSSFCell.CELL_TYPE_NUMERIC:                    if ("@".equals(cell.getCellStyle().getDataFormatString())) {                        value = df.format(cell.getNumericCellValue());                    } else if ("General".equals(cell.getCellStyle()                            .getDataFormatString())) {                        value = cell.getNumericCellValue();                    } else {                        value = sdf.format(HSSFDateUtil.getJavaDate(cell                                .getNumericCellValue()));                    }                    break;                case XSSFCell.CELL_TYPE_BOOLEAN:                    value = cell.getBooleanCellValue();                    break;                case XSSFCell.CELL_TYPE_BLANK:                    value = "";                    break;                 case XSSFCell.CELL_TYPE_FORMULA:                        //公式型                          try {                              CellValue cellValue;                              cellValue = evaluator.evaluate(cell);                              switch (cellValue.getCellType()) {              //判断公式类型                                  case XSSFCell.CELL_TYPE_BOOLEAN:                                      value  = String.valueOf(cellValue.getBooleanValue());                                      break;                                  case XSSFCell.CELL_TYPE_NUMERIC:                                      // 处理日期                                        if (DateUtil.isCellDateFormatted(cell)) {                                           SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");                                           Date date = cell.getDateCellValue();                                           value = format.format(date);                                      } else {                                           value  = String.valueOf(cellValue.getNumberValue());                                      }                                      break;                                  case XSSFCell.CELL_TYPE_STRING:                                      value  = cellValue.getStringValue();                                      break;                                  case XSSFCell.CELL_TYPE_BLANK:                                      value = "";                                      break;                                  case XSSFCell.CELL_TYPE_ERROR:                                      value = "";                                      break;                                  case XSSFCell.CELL_TYPE_FORMULA:                                      value = "";                                      break;                              }                          }                            catch (Exception e) {                                  value = cell.getStringCellValue().toString();                                  cell.getCellFormula();                              }                              break;                default:                    value = cell.toString();                }                if (value == null || "".equals(value)) {                    value = "";                }                linked.add(value);            }            list.add(linked);        }                }catch (IOException e) {            e.printStackTrace();        }        return list;    }    /**     * 读取Office 2007 excel     * */    private static List<List<Object>> read2007Excel(File file)             {        List<List<Object>> list = new LinkedList<List<Object>>();        // 构造 XSSFWorkbook 对象,strPath 传入文件路径        try{        XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file));        XSSFFormulaEvaluator  evaluator = new XSSFFormulaEvaluator(xwb);        // 读取第一章表格内容        XSSFSheet sheet = xwb.getSheetAt(0);        Object value = null;        XSSFRow row = null;        XSSFCell cell = null;        int counter = 0;        for (int i = sheet.getFirstRowNum(); counter < sheet                .getPhysicalNumberOfRows(); i++) {            row = sheet.getRow(i);            if (row == null) {                continue;            } else {                counter++;            }            List<Object> linked = new LinkedList<Object>();//            for (int j = row.getFirstCellNum(); j <= 17; j++) {                for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {                cell = row.getCell(j);                if (cell == null) {                    continue;                }                DecimalFormat df = new DecimalFormat("0");// 格式化 number String                                                            // 字符                SimpleDateFormat sdf = new SimpleDateFormat(                        "yyyy-MM-dd");// 格式化日期字符串                switch (cell.getCellType()) {                case XSSFCell.CELL_TYPE_STRING:                                        value = cell.getStringCellValue();                    break;                case XSSFCell.CELL_TYPE_NUMERIC:                        if ("@".equals(cell.getCellStyle().getDataFormatString())) {                        value = df.format(cell.getNumericCellValue());                    } else if ("General".equals(cell.getCellStyle()                            .getDataFormatString())) {                        value = cell.getNumericCellValue();                    } else {                        value = sdf.format(HSSFDateUtil.getJavaDate(cell                                .getNumericCellValue()));                    }                    break;                case XSSFCell.CELL_TYPE_BOOLEAN:                        value = cell.getBooleanCellValue();                    break;                case XSSFCell.CELL_TYPE_BLANK:                    value = "";                    break;                 case XSSFCell.CELL_TYPE_FORMULA:                        //公式型                          try {                              CellValue cellValue;                              cellValue = evaluator.evaluate(cell);                              switch (cellValue.getCellType()) {              //判断公式类型                                  case XSSFCell.CELL_TYPE_BOOLEAN:                                      value  = String.valueOf(cellValue.getBooleanValue());                                      break;                                  case XSSFCell.CELL_TYPE_NUMERIC:                                      // 处理日期                                        if (DateUtil.isCellDateFormatted(cell)) {                                           SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");                                           Date date = cell.getDateCellValue();                                           value = format.format(date);                                      } else {                                           value  = String.valueOf(cellValue.getNumberValue());                                      }                                      break;                                  case XSSFCell.CELL_TYPE_STRING:                                      value  = cellValue.getStringValue();                                      break;                                  case XSSFCell.CELL_TYPE_BLANK:                                      value = "";                                      break;                                  case XSSFCell.CELL_TYPE_ERROR:                                      value = "";                                      break;                                  case XSSFCell.CELL_TYPE_FORMULA:                                      value = "";                                      break;                              }                          }                            catch (Exception e) {                                  value = cell.getStringCellValue().toString();                                  cell.getCellFormula();                              }                              break;                default:                    value = cell.toString();                }                if (value == null || "".equals(value)) {                    value = "";                }                linked.add(value);            }            list.add(linked);            }        }catch (IOException e) {            e.printStackTrace();        }        return list;    }


0 0
原创粉丝点击