/** * 指定した列番号の<b>XSSFCell</b>を取得する。有効範囲外のセルの場合は新規作成する。 * * @param row <b>XSSFRow</b> * @param nColumn 取得したいセルの列番号 * @return <b>XSSFCell</b> */ public static XSSFCell getCellAnyway(XSSFRow row, int nColumn) { assert row != null; XSSFCell cell = row.getCell(nColumn); if (cell == null) { cell = row.createCell(nColumn); } return cell; }
/** * 指定行の有効列数を取得する * * @param row 行データ * @return 有効列数 */ public static int getColumnCount(XSSFRow row) { if (row != null) { for (int nColumn = 0; ; nColumn++) { XSSFCell cell = row.getCell(nColumn); if (cell == null) { return nColumn; } } } return 0; }
/** * セルの値を取得する * * @param row 行データ * @param nColumn 列番号 * @return セルの値 */ public static Object getData(XSSFRow row, int nColumn) { if (row != null) { XSSFCell cell = row.getCell(nColumn); if (cell != null) { if (XSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) { return cell.getNumericCellValue(); } else if (XSSFCell.CELL_TYPE_STRING == cell.getCellType()) { return cell.getStringCellValue(); } } } return null; }
/** * 列方向のセルの値を合算する * * @param sheet 編集対象シート * @param nColumn 行番号 * @param nStartRow 開始列番号 * @param nEndRow 終了列番号 * @return 合算値 */ public static int sumColumn(XSSFSheet sheet, int nColumn, int nStartRow, int nEndRow) { int sum = 0; for (int nIndex = nStartRow; nIndex <= nEndRow; nIndex++) { XSSFRow row = sheet.getRow(nIndex); assert row != null; XSSFCell cell = row.getCell(nColumn); assert cell != null; if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { sum += cell.getNumericCellValue(); } } return sum; }
/** * 指定セルの削除 * * @param sheet シート * @param startRow 開始行番号 * @param endRow 終了行番号 * @param startColumn 開始列番号 * @param endColumn 終了列番号 */ public static void removeCell( XSSFSheet sheet, int startRow, int endRow, int startColumn, int endColumn) { assert sheet != null; for (int nRow = startRow; nRow <= endRow; nRow++) { XSSFRow row = sheet.getRow(nRow); if (row != null) { for (int nColumn = startColumn; nColumn <= endColumn; nColumn++) { XSSFCell cell = row.getCell(nColumn); if (cell != null) { row.removeCell(cell); } } } } }
/** * 上線は太線のセル行を探す * * @param nRow 行データ * @return 有効列数 */ public static int getRowForBold(XSSFSheet sheet, int nRow, int pageRowNum) { int nRowIndex = nRow; for (nRowIndex = nRow; nRowIndex > (nRow - pageRowNum); nRow--) { XSSFRow row = OoxmlUtil.getRowAnyway(sheet, nRow); if (row != null) { XSSFCell cell = row.getCell(0); XSSFCellStyle styletmp = cell.getCellStyle(); short borderTopnum = styletmp.getBorderTop(); short borderBold = XSSFCellStyle.BORDER_MEDIUM; if (styletmp.getBorderTop() == (XSSFCellStyle.BORDER_MEDIUM)) { break; } } } return nRowIndex; }
/** * セルに設定された計算式を評価して値を取得する * * @param nRow 行番号 * @param nColumn 列番号 * @return セルの値 */ public static Object getDataByEvaluateFormula(XSSFSheet sheet, int nRow, int nColumn) { assert sheet != null; XSSFRow row = getRowAnyway(sheet, nRow); if (row != null) { XSSFCell cell = row.getCell(nColumn); if (cell != null) { FormulaEvaluator eval = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator(); if (eval != null) { CellValue value = eval.evaluate(cell); if (value != null) { return value.getNumberValue(); } } } } return null; }