/** * 取得单元格的值 * @param cell * @return * @throws IOException */ private static Object getCellValue(HSSFCell cell) throws IOException { Object value = ""; if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { value = cell.getRichStringCellValue().toString(); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); value = sdf.format(date); } else { double value_temp = (double) cell.getNumericCellValue(); BigDecimal bd = new BigDecimal(value_temp); BigDecimal bd1 = bd.setScale(3, bd.ROUND_HALF_UP); value = bd1.doubleValue();
/* DecimalFormat format = new DecimalFormat("#0.###"); value = format.format(cell.getNumericCellValue()); */ } } if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) { value = ""; } return value; }
/** * 判断单元格在不在合并单元格范围内&#xff0c;如果是&#xff0c;获取其合并的列数。 * &#64;param sheet 工作表 * &#64;param cellRow 被判断的单元格的行号 * &#64;param cellCol 被判断的单元格的列号 * &#64;return * &#64;throws IOException */ private static int getMergerCellRegionCol(HSSFSheet sheet, int cellRow,int cellCol) throws IOException { int retVal &#61; 0; int sheetMergerCount &#61; sheet.getNumMergedRegions(); for (int i &#61; 0; i CellRangeAddress cra &#61; (CellRangeAddress) sheet.getMergedRegion(i); int firstRow &#61; cra.getFirstRow(); // 合并单元格CELL起始行 int firstCol &#61; cra.getFirstColumn(); // 合并单元格CELL起始列 int lastRow &#61; cra.getLastRow(); // 合并单元格CELL结束行 int lastCol &#61; cra.getLastColumn(); // 合并单元格CELL结束列 if (cellRow >&#61; firstRow && cellRow <&#61; lastRow) { // 判断该单元格是否是在合并单元格中 if (cellCol >&#61; firstCol && cellCol <&#61; lastCol) { retVal &#61; lastCol - firstCol&#43;1; // 得到合并的列数 break; } } } return retVal; }
/** * 判断单元格是否是合并的单格&#xff0c;如果是&#xff0c;获取其合并的行数。 * &#64;param sheet 表单 * &#64;param cellRow 被判断的单元格的行号 * &#64;param cellCol 被判断的单元格的列号 * &#64;return * &#64;throws IOException */ private static int getMergerCellRegionRow(HSSFSheet sheet, int cellRow,int cellCol) throws IOException { int retVal &#61; 0; int sheetMergerCount &#61; sheet.getNumMergedRegions(); for (int i &#61; 0; i CellRangeAddress cra &#61; (CellRangeAddress) sheet.getMergedRegion(i); int firstRow &#61; cra.getFirstRow(); // 合并单元格CELL起始行 int firstCol &#61; cra.getFirstColumn(); // 合并单元格CELL起始列 int lastRow &#61; cra.getLastRow(); // 合并单元格CELL结束行 int lastCol &#61; cra.getLastColumn(); // 合并单元格CELL结束列 if (cellRow >&#61; firstRow && cellRow <&#61; lastRow) { // 判断该单元格是否是在合并单元格中 if (cellCol >&#61; firstCol && cellCol <&#61; lastCol) { retVal &#61; lastRow - firstRow &#43; 1; // 得到合并的行数 break; } } } return retVal; }
/** * 单元格背景色转换 * &#64;param hc * &#64;return */ private String convertToStardColor(HSSFColor hc) { StringBuffer sb &#61; new StringBuffer(""); if (hc !&#61; null) { int a &#61; HSSFColor.AUTOMATIC.index; int b &#61; hc.getIndex(); if (a &#61;&#61; b) { return null; } sb.append("#"); for (int i &#61; 0; i String str ; String str_tmp &#61; Integer.toHexString(hc.getTriplet()[i]); if (str_tmp !&#61; null && str_tmp.length() <2) { str &#61; "0" &#43; str_tmp; }else { str &#61; str_tmp; } sb.append(str); } } return sb.toString(); }