Apache-POI操作Excel的一些小技巧
Apache-POI操作Excel将合并后的单元格全部填充为相同数据的一个实例。private static void fillMergedRegion(final Sheet sheet) { for (int i = 0; i < sheet.getNumMergedRegions(); i++) { int startRow = sheet.getMergedRegion(i).getFirstRow(); int endRow = sheet.getMergedRegion(i).getLastRow(); int startColumn = sheet.getMergedRegion(i).getFirstColumn(); int endColumn = sheet.getMergedRegion(i).getLastColumn(); String value = getStringValue(sheet.getRow(startRow).getCell(startColumn)); for (int row = startRow; row <= endRow; row++) { for (int column = startColumn; column <= endColumn; column++) { sheet.getRow(row).getCell(column).setCellValue(value); } } } }
Apache-POI操作Excel获得单元格内容
public static String getStringValue(Cell cell) { if (cell == null) { return StringConst.EMPTY_STRING; } // get the type of cell, and transform it if (Cell.CELL_TYPE_FORMULA == cell.getCellType()) { switch (cell.getCachedFormulaResultType()) { // if it is mumeric type case Cell.CELL_TYPE_NUMERIC: return new DataFormatter().createFormat(cell).format(cell.getNumericCellValue()); // if it is string type case Cell.CELL_TYPE_STRING: return cell.getRichStringCellValue().toString(); } } return new DataFormatter().formatCellValue(cell); }
补充:软件开发 , Java ,