【Java】Apache POIを使ったExcelファイル操作
ダウンロード
Apache POIプロジェクトサイトから、poi-bin-3.13-20150929.zipをダウンロードする。解凍後、必要なjarファイルをプロジェクトに追加する。
- poi-3.13-20150929.jar
- poi-ooxml-3.13-20150929.jar
- poi-ooxml-schemas-3.13-20150929.jar
- xmlbeans-2.6.0.jar
注意
エクセル操作
ファイル読み込み・出力
try (FileInputStream in = new FileInputStream("XXXX.xlsx")) { Workbook book = WorkbookFactory.create(in); Sheet sheet = book.getSheetAt(0); // データ追加 // Excelファイル出力 try (FileOutputStream out = new FileOutputStream("YYYY.xlsx")) { book.write(out); } } catch (Exception e) { // エラー処理 }
セル操作
- セルは、Row/Cellという順でアクセスする。
- Row/Cellは必ずしも存在するわけではないので、アクセスする場合にはnullチェックする。
- エクセルのデータタイプは、セルに追加するデータ型(Integer, String)に対応する。
セルに値を設定する
Row row = sheet.getRow(rowIndex); if (row != null) { Cell cell = row.getCell(colIndex); if (cell != null) { cell.setCellValue(value); } }
セルに値を追加する
Row row = sheet.getRow(rowIndex); if (row != null) { Cell cell = row.getCell(colIndex); if (cell == null) { cell = row.createCell(colIndex); } cell.setCellValue(value); }
カラムを削除する
POIには、エクセルのカラムを削除する機能はない。すべてのRowに対してカラムを削除する、カラムのセルを削除する。
public static void deleteColumn(Sheet sheet, int fromColumn, int toColumn){ for ( int r = 0; r < sheet.getLastRowNum() + 1; r++ ){ Row row = sheet.getRow(r); if ( row == null ) { continue; } int lastColumn = row.getLastCellNum(); if (lastColumn < fromColumn ) { continue; } if (lastColumn < toColumn) { toColumn = lastColumn; } for ( int c = fromColumn + 1; c < lastColumn + 1; c++ ){ Cell cell = row.getCell(c-1); if ( cell != null ) { row.removeCell(cell); } } } }
セルに2重罫線を設定する
セルのスタイルプロパティを設定する。 ここで、セルの位置、プロパティはCellUtilクラスで定義されている以下の値を使う。
(CellUtil (POI API Documentation))で定義されているがコメントがないので想像するしかない。
セル位置- BORDER_LEFT
- BORDER_RIGHT
- BORDER_TOP
- BORDER_BOTTOM
- BORDER_NONE
- BORDER_THIN
- BORDER_MEDIUM
- BORDER_DASHED
- BORDER_DOTTED
- BORDER_THICK
- BORDER_DOUBLE
- BORDER_HAIR
- BORDER_MEDIUM_DASHED
- BORDER_DASH_DOT
- BORDER_MEDIUM_DASH_DOT
- BORDER_DASH_DOT_DOT
- BORDER_MEDIUM_DASH_DOT_DOT
- BORDER_SLANTED_DASH_DOT
public static void setCellStyle(Sheet sheet, int rowIndex, int colIndex, String pos, short val) { Row row = sheet.getRow(rowIndex); if (row != null) { Cell cell = row.getCell(colIndex); if (cell != null) { CellUtil.setCellStyleProperty(cell, cell.getSheet().getWorkbook(), pos, val); } } }
セルに背景色を設定する
IndexedColorsクラスに色の定義がある。
IndexedColors (POI API Documentation) 例) 白を指定する場合には、IndexedColors.WHITE.getIndex()を使う。
public static void setCellColor(Sheet sheet, int rowIndex, int colIndex, short color) { Row row = sheet.getRow(rowIndex); if (row != null) { Cell cell = row.getCell(colIndex); if (cell != null) { CellStyle style = cell.getCellStyle(); style.setFillForegroundColor(color); } } }
セルにフォントを設定する
CellUtilを使わない場合、他のセルのフォントも変わってしまうのでCellUtilクラスを使う必要がある。
public static void setNormalFont(Sheet sheet, int rowIndex, int colIndex) { Font font = sheet.getWorkbook().createFont(); font.setBold(false); font.setFontName("MS Pゴシック"); Row row = sheet.getRow(rowIndex); if (row != null) { Cell cell = row.getCell(colIndex); if (cell != null) { CellUtil.setFont(cell, sheet.getWorkbook(), font); CellStyle style = cell.getCellStyle(); style.setFont(font); } } }