/** * 指定行を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); } }
/** * セルに文字列を出力する * * @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); } } } }
/** * ハイパーリンクの設定 * * @param sheet シート * @param nRow 対象行番号 * @param nColumn 対象列番号 * @param value ハイパーリンクテキスト * @param url ハイパーリンク先URL */ public static void setHyperLink( XSSFSheet sheet, int nRow, int nColumn, String value, String url) { assert sheet != null; XSSFWorkbook workbook = sheet.getWorkbook(); CreationHelper helper = workbook.getCreationHelper(); Hyperlink hyperlink = helper.createHyperlink(Hyperlink.LINK_URL); hyperlink.setAddress(url); XSSFRow row = getRowAnyway(sheet, nRow); XSSFCell cell = getCellAnyway(row, nColumn); cell.setCellValue(value); cell.setHyperlink(hyperlink); // ハイパーリンクテキストの装飾 XSSFFont font = workbook.createFont(); XSSFCellStyle style = workbook.createCellStyle(); // font.setColor(new XSSFColor(new Color(0, 0, 255))); font.setUnderline(XSSFFont.U_SINGLE); style.setFont(font); cell.setCellStyle(style); }
private void generateExcelDoc(String docName) throws FileNotFoundException, IOException { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet overall = workbook.createSheet("Overall"); XSSFRow row = overall.createRow(0); XSSFCellStyle topStyle = workbook.createCellStyle(); topStyle.setAlignment(CellStyle.ALIGN_CENTER); XSSFCell theme = row.createCell(0); theme.setCellValue("Theme"); overall.autoSizeColumn(0); XSSFCell occurs = row.createCell(1); occurs.setCellValue("Occurrences"); overall.autoSizeColumn(1); XSSFCell prev = row.createCell(2); prev.setCellValue("Prevalence"); overall.autoSizeColumn(2); theme.setCellStyle(topStyle); occurs.setCellStyle(topStyle); prev.setCellStyle(topStyle); for (int i = 0; i < themes.size(); i++) { XSSFRow r = overall.createRow((i + 1)); XSSFCell c = r.createCell(0); c.setCellValue(themes.get(i).getName()); XSSFCell c1 = r.createCell(1); c1.setCellValue(themes.get(i).getTotalOccurs()); XSSFCell c2 = r.createCell(2); c2.setCellValue(calculatePrevalence(themes.get(i).getTotalOccurs(), lineCount)); } // This could be done in the previous loop but since we don't need // indices as much, we may as well use the cleaner for each loop for (Theme t : themes) { XSSFSheet themeSheet = workbook.createSheet(t.getName()); XSSFRow row1 = themeSheet.createRow(0); XSSFCell keyword = row1.createCell(0); keyword.setCellValue("Keyword"); keyword.setCellStyle(topStyle); XSSFCell occ = row1.createCell(1); occ.setCellValue("Occurrences"); occ.setCellStyle(topStyle); XSSFCell themePrev = row1.createCell(2); themePrev.setCellValue("Prevalence"); themePrev.setCellStyle(topStyle); for (int i = 0; i < t.getKeywords().size(); i++) { Keyword k = t.getKeywords().get(i); XSSFRow r = themeSheet.createRow((i + 1)); XSSFCell c = r.createCell(0); c.setCellValue(k.getName()); XSSFCell c1 = r.createCell(1); c1.setCellValue(k.getNumOccurs()); XSSFCell c2 = r.createCell(2); c2.setCellValue(calculatePrevalence(k.getNumOccurs(), t.getTotalOccurs())); } } FileOutputStream output = new FileOutputStream(docName); workbook.write(output); output.close(); }