/** * セルに文字列を出力する * * @param cell 対象セル * @param object 出力データ * @param style セルスタイル * @param zeroValue 値が0の時に設定する値 */ private static void setData(XSSFCell cell, Object object, XSSFCellStyle style, String zeroValue) { if (style != null) { cell.setCellStyle(style); } if (object instanceof String) { cell.setCellType(XSSFCell.CELL_TYPE_STRING); cell.setCellValue((String) object); } else if (object instanceof Integer) { Integer integer = (Integer) object; if (0 == integer) { cell.setCellType(XSSFCell.CELL_TYPE_STRING); cell.setCellValue(zeroValue); } else { cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue((Integer) object); } } else if (object instanceof Double) { cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC); if (Double.isNaN((Double) object)) { cell.setCellValue(zeroValue); } else { Double value = (Double) object; if (0 == value.compareTo((Double) 0.0)) { cell.setCellValue(zeroValue); } else { cell.setCellValue((Double) object); } } } }
/** * 指定行を0で埋める * * @param sheet 編集対象シート * @param nRow 行番号 * @param nStartColumn 開始列番号 * @param nEndColumn 終了列番号 */ public static void setZero(XSSFSheet sheet, int nRow, int nStartColumn, int nEndColumn) { XSSFRow row = getRowAnyway(sheet, nRow); for (int nIndex = nStartColumn; nIndex <= nEndColumn; nIndex++) { XSSFCell cell = getCellAnyway(row, nIndex); cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue((double) 0); } }
/** * Use this to create new cells within the row and return it. * * @param columnIndex - the column number this cell represents * @param type - the cell's data type * @return XSSFCell a high level representation of the created cell. * @throws IllegalArgumentException if the specified cell type is invalid, columnIndex < 0 or * greater than 16384, the maximum number of columns supported by the SpreadsheetML format * (.xlsx) * @see Cell#CELL_TYPE_BLANK * @see Cell#CELL_TYPE_BOOLEAN * @see Cell#CELL_TYPE_ERROR * @see Cell#CELL_TYPE_FORMULA * @see Cell#CELL_TYPE_NUMERIC * @see Cell#CELL_TYPE_STRING */ public XSSFCell createCell(int columnIndex, int type) { CTCell ctCell; XSSFCell prev = _cells.get(columnIndex); if (prev != null) { ctCell = prev.getCTCell(); ctCell.set(CTCell.Factory.newInstance()); } else { ctCell = _row.addNewC(); } XSSFCell xcell = new XSSFCell(this, ctCell); xcell.setCellNum(columnIndex); if (type != Cell.CELL_TYPE_BLANK) { xcell.setCellType(type); } _cells.put(columnIndex, xcell); return xcell; }