図形
POIでは「Microsoft Office drawing tools」を使用した図形の描画をサポートし、ワークシート上の図形は階層構造の形式で管理されています。HSSFコンポーネントでは、階層の1番上に「HSSFPatriarch」と呼ばれるオブジェクトがあり、他の図形やグループのコンテナの役割をしています。
POIで図形を描く場合、まずワークシートオブジェクトからこのコンテナを生成します。OOXML形式に対応したXSSFSheetの場合は「HSSFPatriarch」と同等の役割を担う「XSSFDrawing」を生成する必要があります。次に「XSSFDrawing」に図形を配置したい位置情報を渡して、図形を生成するメソッドを呼びます。
ちなみに図形が既に存在するワークブックを読込後に「XSSFDrawing」を生成した場合、既存の図形は消去されてしまいます。それでは「XSSFDrawing」を利用した図形の描画方法をサンプルプログラム(ShapeSample)で確認してみましょう。
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFShape;
import org.apache.poi.ss.usermodel.ShapeTypes;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ShapeSample {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// Excelファイルを表すワークブックオブジェクトを生成
XSSFWorkbook wb = new XSSFWorkbook();
// セルスタイルの生成と設定
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
style.setWrapText(true);
// フォントの生成と設定
XSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 18);
font.setFontName("Times New Roman");
font.setItalic(true);
// ワークシート・行・セルの生成と設定
XSSFSheet sheet = wb.createSheet("Shape.xlsx");
XSSFCell cell = sheet.createRow(1).createCell(1);
cell.setCellValue("This merged region is surrounded by a red rectangle");
cell.setCellStyle(style);
// (1)セルの結合
sheet.addMergedRegion(new CellRangeAddress(
1, //first row(0-based)
3, //last row(0-based)
1, //first column(0-based)
5 //last column(0-based)
));
// (2)XSSFDrawingの生成
XSSFDrawing patriarch = sheet.createDrawingPatriarch();
// (3)XSSFClientAnchorの生成
XSSFClientAnchor anchor = new XSSFClientAnchor(
0, //dx1 the x coordinate within the first cell
0, //dy1 the y coordinate within the first cell
0, //dx2 the x coordinate within the second cell
0, //dy2 the y coordinate within the second cell
1, //col1 the column (0 based) of the first cell
1, //row1 the row (0 based) of the first cell
6, //col2 the column (0 based) of the second cell
4 //row2 the row (0 based) of the second cell.
);
XSSFClientAnchor anchor2 = new XSSFClientAnchor(0, 0, 0, 0, 3, 5, 4, 8);
XSSFClientAnchor anchor3 = new XSSFClientAnchor(0, 0, 0, 0, 1, 9, 6, 11);
// (4)XSSFSimpleShapeの生成
XSSFSimpleShape simpleShape = patriarch.createSimpleShape(anchor);
XSSFSimpleShape simpleShape2 = patriarch.createSimpleShape(anchor2);
XSSFSimpleShape textBox = patriarch.createTextbox(anchor3);
// (5)長方形を設定
simpleShape.setShapeType(ShapeTypes.RECT);
simpleShape.setLineStyleColor(255, 0, 0);
simpleShape.setNoFill(true);
simpleShape.setLineWidth(2.5);
simpleShape.setLineStyle(HSSFShape.LINESTYLE_LONGDASHDOTDOTGEL);
// (6)下向きの矢印を選択
simpleShape2.setShapeType(ShapeTypes.DOWN_ARROW);
simpleShape2.setFillColor(153, 051, 102);
simpleShape2.setLineWidth(0.0);
// (7)テキストボックスの設定
XSSFRichTextString richString = new XSSFRichTextString(
"This is a RichTextString in a Textbox");
richString.applyFont(10, 24, font);
textBox.setText(richString);
// (8)ワークブックオブジェクトをファイル出力
FileOutputStream fileOut = new FileOutputStream("ShapeSample.xlsx");
wb.write(fileOut);
fileOut.close();
}
}
- ソース4 -(1)
- ソース4 -(2)
- ソース4 -(3)
- ソース4 -(4)
- ソース4 -(5)
- ソース4 -(6)
- ソース4 -(7)
セルの範囲を表す「CellRangeAddress」を引数にして、ワークシートオブジェクトの「addMergedRegion」メソッドを利用すると指定したセルの範囲が結合されます。
ワークシートオブジェクトから階層構造の1番上にあるコンテナである「XSSFDrawing」を生成します。
図形をワークシートのどの位置に配置するかの情報を表す「XSSFClientAnchor」を生成します。図形が配置される範囲の左上と右下の位置を指定しています。
「XSSFDrawing」に位置情報を渡して、図形を生成しています。最初の2つは「XSSFSimpleShape」オブジェクトを、3つ目は「XSSFSimpleShape」のサブクラスである「テキストボックス」を生成しています。
「XSSFSimpleShape」オブジェクトに属性を設定しています。「setShapeType(int type)」で図形の種類を選択できます。ここでは長方形を選択していますが、ShapeTypesクラスには187種類の図形の定数が用意されています。その他に線の色、塗りつぶしの有無、線の幅、線の種類が指定できます。
「XSSFSimpleShape」オブジェクトに対して、図形の種類に「下向きの矢印」を設定しています。
RichTextStringを生成し「テキストボックス」に追加しています。追加したテキストのうち、文字列「RichTextString」にはフォント名「Times New Roman」でスタイルが「斜体」の書式設定をしています。
その他の文字列にはフォント名「Arial」でスタイルは「標準」がデフォルトで設定されています。RichTextStringを利用すればこのような装飾も簡単にできます。

