2010年5月13日木曜日

groovyとApache POIを使用してセルの罫線の色を設定する

groovyとApache POIを使用してセルの罫線の色を設定するには、以下のコードを実行します。

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;

workbook = WorkbookFactory.create(new FileInputStream("test1.xlsx"))
// セルの罫線の色を設定
sheet = workbook.getSheetAt(0)
cs = workbook.createCellStyle()
// 上側
cs.setTopBorderColor(IndexedColors.ORANGE.getIndex())
cs.setBorderTop(CellStyle.BORDER_THICK)
// 下側
cs.setBottomBorderColor(IndexedColors.ORANGE.getIndex())
cs.setBorderBottom(CellStyle.BORDER_THICK)
// 左側
cs.setLeftBorderColor(IndexedColors.ORANGE.getIndex())
cs.setBorderLeft(CellStyle.BORDER_THICK)
// 右側
cs.setRightBorderColor(IndexedColors.ORANGE.getIndex())
cs.setBorderRight(CellStyle.BORDER_THICK)
sheet.getRow(1).getCell(1).setCellStyle(cs)

workbook.write(new FileOutputStream("test24.xlsx"))


サンプルExcelブック(test1.xlsx)


出力Excel(test24.xlsx)


動作環境
groovy 1.7.0, JDK6 Update18, Apache POI 3.6

0 件のコメント:

コメントを投稿