LoginSignup
8
7

More than 5 years have passed since last update.

POIでxlsxファイルのカスタムプロパティを操作する

Posted at

プログラムから作成したxlsxファイルに同定用のデータをどうにか仕込んでおく必要があったので調べてみたら、Officeにはカスタムプロパティというメタデータを保存しておく仕組みがあったのでそれを使うことにした

POIからはこんな感じでアクセスできる

public String test() throws Exception {
    XSSFWorkbook xssfWorkbook = (XSSFWorkbook) WorkbookFactory.create(new FileInputStream("hoge.xlsx"));

    // 値の設定
    xssfWorkbook.getProperties().getCustomProperties().addProperty("hoge", "fuga");
        // 値の取得
    for (CTProperty ctProperty : xssfWorkbook.getProperties().getCustomProperties().getUnderlyingProperties().getPropertyList()) {
        if (ctProperty.getName().equals("hoge")) {
            return ctProperty.getLpwstr();
        }
    }
    return null;
}

文字列取得メソッドはgetLpwstrの他にもgetBstrgetLpstrがあるけど、ここを見る限りJavaから読み書きするならUnicodeのgetLpwstr使えばよさそう

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7