Exemplo n.º 1
0
  /**
   * 罫線スタイルの<b>CellStyle</b>を生成 1行のみ描画する
   *
   * @param workbook ワークブック
   * @param sheet シート
   * @param nRow 行
   * @param nColumn       列
   * @param isBold 太字フラグ
   * @param fontSize 文字サイズ
   * @param fontHeight 行高
   */
  public static void setCellStyleForLabel(
      XSSFWorkbook workbook,
      XSSFSheet sheet,
      int nRow,
      int nColumn,
      boolean isBold,
      short fontSize,
      float fontHeight) {
    assert sheet != null;

    // style設定
    XSSFCellStyle style = workbook.createCellStyle();
    XSSFFont font = workbook.createFont();
    if (isBold) {
      font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); // 文字太字
    }
    font.setFontHeightInPoints((short) fontSize); // 文字サイズ
    font.setFontName(DEFAULT_FONT_NAME);
    style.setFont(font); // 文字太字 と 文字サイズ

    style.setAlignment(CellStyle.ALIGN_GENERAL); // 水平方向の標準
    style.setVerticalAlignment(CellStyle.VERTICAL_TOP); // 垂直方向の上詰め
    style.setWrapText(true); // 折り返して全体を表示する

    // セルに罫線を描画
    XSSFRow row = getRowAnyway(sheet, nRow);
    XSSFCell cell = getCellAnyway(row, nColumn);
    cell.setCellStyle(style);
    row.setHeightInPoints(fontHeight); // 行高設定
  }
Exemplo n.º 2
0
 /**
  * 罫線スタイルの<b>CellStyle</b>を生成
  *
  * @param workbook ワークブック
  * @return <b>CellStyle</b>
  */
 public static XSSFCellStyle createTableDataCellStyle(XSSFWorkbook workbook) {
   XSSFCellStyle style = workbook.createCellStyle();
   style.setBorderTop(XSSFCellStyle.BORDER_THIN);
   style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
   style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
   style.setBorderRight(XSSFCellStyle.BORDER_THIN);
   return style;
 }
Exemplo n.º 3
0
 /**
  * デフォルトのセルスタイルを作成
  *
  * @param workbook ワークブック
  * @param bold 太字設定フラグ
  * @param centering センタリングフラグ
  * @param fontSize フォントサイズ
  * @return <b>CellStyle</b>
  */
 public static XSSFCellStyle createDefaultCellStyle(
     XSSFWorkbook workbook, boolean bold, boolean centering, int fontSize) {
   XSSFFont font = workbook.createFont();
   if (bold) {
     font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
   }
   font.setFontHeightInPoints((short) fontSize);
   font.setFontName(DEFAULT_FONT_NAME);
   XSSFCellStyle style = workbook.createCellStyle();
   style.setFont(font);
   if (centering) {
     style.setAlignment(CellStyle.ALIGN_CENTER_SELECTION);
   }
   return style;
 }
Exemplo n.º 4
0
 /**
  * 上線は太線のセル行を探す
  *
  * @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;
 }
Exemplo n.º 5
0
 /**
  * ハイパーリンクの設定
  *
  * @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);
 }
Exemplo n.º 6
0
 /**
  * 罫線スタイルの<b>CellStyle</b>を生成
  *
  * @param workbook ワークブック
  * @param backgroundColor 背景色
  * @return <b>CellStyle</b>
  */
 public static XSSFCellStyle createTableDataCellStyle(
     XSSFWorkbook workbook, Color backgroundColor) {
   XSSFCellStyle style = workbook.createCellStyle();
   style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); // 必ずsetFillForegroundColorの直前に記述すること
   style.setFillForegroundColor(new XSSFColor(backgroundColor));
   style.setBorderTop(XSSFCellStyle.BORDER_THIN);
   style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
   style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
   style.setBorderRight(XSSFCellStyle.BORDER_THIN);
   return style;
 }
Exemplo n.º 7
0
 /**
  * デフォルトのテーブルヘッダースタイルを作成
  *
  * @param workbook ワークブック
  * @param bold 太字設定フラグ
  * @param fontSize フォントサイズ
  * @param backgroundColor 背景色
  * @return <b>CellStyle</b>
  */
 public static XSSFCellStyle createDefaultTableHeaderCellStyle(
     XSSFWorkbook workbook, boolean bold, boolean center, int fontSize, Color backgroundColor) {
   XSSFFont font = workbook.createFont();
   if (bold) {
     font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
   }
   font.setFontHeightInPoints((short) fontSize);
   font.setFontName(DEFAULT_FONT_NAME);
   XSSFCellStyle style = workbook.createCellStyle();
   if (center) {
     style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
   }
   style.setFont(font);
   style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); // 必ずsetFillForegroundColorの直前に記述すること
   style.setFillForegroundColor(new XSSFColor(backgroundColor));
   style.setBorderTop(XSSFCellStyle.BORDER_THIN);
   style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
   style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
   style.setBorderRight(XSSFCellStyle.BORDER_THIN);
   return style;
 }
Exemplo n.º 8
0
  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();
  }
Exemplo n.º 9
0
  /**
   * 罫線スタイルの<b>CellStyle</b>を生成
   *
   * @param workbook ワークブック
   * @param backgroundColor 背景色
   * @return <b>CellStyle</b>
   */
  public static XSSFCellStyle createTableDataCellStyle(
      XSSFWorkbook workbook,
      boolean isTop,
      boolean isBottom,
      boolean isLeft,
      boolean isRight,
      Color backgroundColor,
      short fontSize,
      boolean bold) {
    XSSFCellStyle style = workbook.createCellStyle();
    style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); // 必ずsetFillForegroundColorの直前に記述すること
    style.setFillForegroundColor(new XSSFColor(backgroundColor));
    style.setAlignment(CellStyle.ALIGN_LEFT); // 水平方法の位置
    style.setVerticalAlignment(CellStyle.VERTICAL_TOP);
    style.setWrapText(true); // 折り返して全体を表示する
    //        style.setShrinkToFit(true);//縮小して全体を表示する

    // 文字サイズ設定
    XSSFFont font = workbook.createFont();
    font.setFontHeightInPoints(fontSize);
    if (bold) {
      font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
    }
    style.setFont(font);

    if (isTop) {
      style.setBorderTop(XSSFCellStyle.BORDER_MEDIUM);
    } else {
      style.setBorderTop(XSSFCellStyle.BORDER_THIN);
    }
    if (isBottom) {
      style.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM);
    } else {
      style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
    }
    if (isLeft) {
      style.setBorderLeft(XSSFCellStyle.BORDER_MEDIUM);
    } else {
      style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
    }
    if (isRight) {
      style.setBorderRight(XSSFCellStyle.BORDER_MEDIUM);
    } else {
      style.setBorderRight(XSSFCellStyle.BORDER_THIN);
    }
    return style;
  }