解説動画
今回は、Apache POIを使って、Excelのピボットテーブルを作ります。
 ピボットテーブルは便利なので、試してみてくださいね。
■動画はこちら
■Youtube版の解説で使用しているソースコード
 動画と一緒にこちらも参考にどうぞ。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package poi; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.DataConsolidateFunction; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.usermodel.XSSFPivotTable; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbookFactory; public class ExcelPivotTest {     public static void main(String[] args) {         //読み込み(Book1.xlsx)と書き込み(Book2.xlsx)の準備         try (FileInputStream fis = new FileInputStream("D:\\work\\Book1.xlsx");             XSSFWorkbook wb = new XSSFWorkbookFactory().create(fis);             FileOutputStream fos = new FileOutputStream("D:\\work\\Book2.xlsx")) {             //ピボットテーブルを出力するシートを作成             XSSFSheet shOut = wb.createSheet("出力");             //ピボットテーブル作成             createPivotTable(wb, shOut);             //Excelファイル書き込み             wb.write(fos);         } catch (Exception e) {             e.printStackTrace();         }     }     private static void createPivotTable(XSSFWorkbook wb, XSSFSheet shOut) {         //データソースのシートを取得         XSSFSheet shSrc = wb.getSheetAt(0);         //データソースシートのデータのセル範囲         AreaReference dataArea = new AreaReference(             new CellReference(0, 0),    //左上             new CellReference(366, 2),    //右下             SpreadsheetVersion.EXCEL2007         );         //ピボットテーブルシートのセル位置(左上)         CellReference crTopLeft = new CellReference(0,0);         //ピボットテーブルを作成する         XSSFPivotTable pivotTable = shOut.createPivotTable(             dataArea,             crTopLeft,             shSrc         );         //行フィールドを追加(=月)         pivotTable.addRowLabel(1);         //列フィールドを追加(=曜日)         pivotTable.addColLabel(2);         //値データを追加(=日付のカウント)         pivotTable.addColumnLabel(DataConsolidateFunction.COUNT, 0);      } } | 














