{Java}エクセル操作

 

https://style.potepan.com/articles/27539.html
https://stackoverflow.com/questions/47881821/error-statuslogger-log4j2-could-not-find-a-logging-implementation
https://www.sejuku.net/blog/21222
https://iid.systems/blog/archives/70

 

想定シナリオ:
6列のエクセル(1行目ヘッダー)から
ヘッダーを除いて1,3,5列のみ取得して、3列目の昇順にソートしてエクセルへ出力


import java.io.*;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.*;


public class a {
  public static void main(String [] args) throws EncryptedDocumentException, IOException{

    Row row;
    Cell cell_0;
    Cell cell_1;
    Cell cell_2;
    Cell cell_3;
    Cell cell_4;
    Cell cell_5;
    int i;

    // 1. テスト用ファイル作成

    // 出力用のストリームを用意
    FileOutputStream ofs1 = new FileOutputStream("a.xlsx");
    // Excelファイルを作成
    Workbook  wb1 = new XSSFWorkbook(); 
    // シートを作成
    Sheet ws1 = wb1.createSheet("Sheet1");

    Random rnd = new Random();

    for(i = 0; i<= 10; i++){
      // 行を作成
      row = ws1.createRow(i);

      // セルを作成
      cell_0 = row.createCell(0);
      cell_1 = row.createCell(1);
      cell_2 = row.createCell(2);
      cell_3 = row.createCell(3);
      cell_4 = row.createCell(4);
      cell_5 = row.createCell(5);

      // セルに値を設定
      if(i == 0){
        cell_0.setCellValue("col1");
        cell_1.setCellValue("col2");
        cell_2.setCellValue("col3");
        cell_3.setCellValue("col4");
        cell_4.setCellValue("col5");
        cell_5.setCellValue("col6");
      } else  {
        cell_0.setCellValue("A"+i);
        cell_1.setCellValue("B"+i);
        cell_2.setCellValue(String.valueOf( rnd.nextInt(100) + 1) );
        cell_3.setCellValue(String.valueOf(i*4) );
        cell_4.setCellValue(String.valueOf(i*5) );
        cell_5.setCellValue(String.valueOf(i*6) );
      }
    }

    // ファイルへ出力
    wb1.write(ofs1);
    wb1.close();


    // 2. ファイル加工
    // シートの内容を取得
    // 1行目が空になったら終了

    HashMap<String, String> [] hashlist = new HashMap[10];
    for ( i = 0; i < hashlist.length; i++){
      hashlist[i] = new HashMap<String, String>();
    }

    // Excelファイルへアクセス
    Workbook wb2 = WorkbookFactory.create(new File("a.xlsx") );
    // シートを取得
    Sheet ws2 = wb2.getSheet("Sheet1");

    i = 0;
    while ( true ){
    row = ws1.getRow(i+1);

    if( row == null ) {
      break;
    }

    // セルの値を取得
    cell_0 = row.getCell(0);
    cell_2 = row.getCell(2);
    cell_4 = row.getCell(4);

    hashlist[i].put("col1", cell_0.getStringCellValue() );
    hashlist[i].put("col2", cell_2.getStringCellValue() );
    hashlist[i].put("col3", cell_4.getStringCellValue() );

    i++;
    }

    wb2.close();


    // 3. 連想配列の配列書き込み

    // 出力用のストリームを用意
    FileOutputStream ofs2 = new FileOutputStream("b.xlsx");
    // Excelファイルを作成
    Workbook  wb3 = new XSSFWorkbook(); 
    // シートを作成
    Sheet ws3 = wb3.createSheet("Sheet1");

    Arrays.sort(hashlist, new Comp("col2") );

    for (i = 0; i < hashlist.length; i++) {
      row = ws3.createRow(i);
      // セルを作成
      cell_0 = row.createCell(0);
      cell_1 = row.createCell(1);
      cell_2 = row.createCell(2);
      // セルに値を設定
      cell_0.setCellValue(hashlist[i].get("col1") );
      cell_1.setCellValue(hashlist[i].get("col2") );
      cell_2.setCellValue(hashlist[i].get("col3") );
    }

    // ファイルへ出力
    wb3.write(ofs2);
    wb3.close();

    // 4. ワークブックデータ確認

    // Excelファイルへアクセス
    Workbook wb4 = WorkbookFactory.create(new File("b.xlsx") );
    // シートを取得
    Sheet ws4 = wb4.getSheet("Sheet1");

    String val_0;
    String val_1;
    String val_2;

    i = 0;
    while ( true ){
      row = ws4.getRow(i);

      if( row == null ) {
        break;
      }

      // セルの値を取得
      cell_0 = row.getCell(0);
      cell_1 = row.getCell(1);
      cell_2 = row.getCell(2);

      // セルの値を文字列として取得
      val_0 = cell_0.getStringCellValue();
      val_1 = cell_1.getStringCellValue();
      val_2 = cell_2.getStringCellValue();

      // 文字列を結果として表示
      System.out.println(val_0+" "+val_1+" "+val_2);

      i++;
    }
    wb4.close();

  }
}

class Comp implements java.util.Comparator<HashMap<String, String>> {

  private String key;

  public Comp(String key){
    this.key=key;
  };

  public int compare(HashMap<String, String> o1, HashMap<String, String> o2) {
    return Integer.parseInt(o1.get(key) ) - Integer.parseInt(o2.get(key) ) > 0 ? 1 : Integer.parseInt(o1.get(key) ) - Integer.parseInt(o2.get(key) ) == 0 ? 0 : -1;
  }
}

 


javac -classpath .;log4j-core-2.20.0.jar;poi-bin-5.2.3/*;poi-bin-5.2.3/lib/*;poi-bin-5.2.3/ooxml-lib/* a.java
java  -classpath .;log4j-core-2.20.0.jar;poi-bin-5.2.3/*;poi-bin-5.2.3/lib/*;poi-bin-5.2.3/ooxml-lib/* a