0
0

More than 3 years have passed since last update.

[Java]  エクセルで数式を追加・読み取る

Posted at

Excelで、セルに数式や関数を入力して計算などの作業を自動的に行わせることができます。数式や関数を使いこなせれば、Excelでの文書作成がぐっと楽になると思いますので、今回は、free Spire.XLS for Javaという無料のライブラリを活用して、Excelで数式を追加・読み取る方法を紹介していきます。

下準備

1.E-iceblueの公式サイトからFree Spire. XLS for Java無料版をダウンロードしてください。

f:id:lendoris:20210818121612p:plain

2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. XLS.jarを参照に追加してください。

f:id:lendoris:20210818121621p:plain

数式を追加

import com.spire.xls.*;

public class InsertFormulas {

    public static void main(String[] args) {

        //Workbook オブジェクトを作成します。
        Workbook workbook = new Workbook();

        //一つ目のシートを取得します。
        Worksheet sheet = workbook.getWorksheets().get(0);

        //ふたつの変数を宣言します。
        int currentRow = 1;
        String currentFormula = null;

        //列の幅を設定します。
        sheet.setColumnWidth(1, 32);
        sheet.setColumnWidth(2, 16);

        //テストデータをセルに書き込みます。
        sheet.getCellRange(currentRow,1).setValue("テストデータ:");
        sheet.getCellRange(currentRow,2).setNumberValue(1);
        sheet.getCellRange(currentRow,3).setNumberValue(2);
        sheet.getCellRange(currentRow,4).setNumberValue(3);
        sheet.getCellRange(currentRow,5).setNumberValue(4);
        sheet.getCellRange(currentRow,6).setNumberValue(5);

        //テキストを書き込みます。
        currentRow += 2;
        sheet.getCellRange(currentRow,1).setValue("数式:") ; ;
        sheet.getCellRange(currentRow,2).setValue("結果:");

        //セルの書式を設定します。
        CellRange range = sheet.getCellRange(currentRow,1,currentRow,2);
        range.getStyle().getFont().isBold(true);
        range.getStyle().setKnownColor(ExcelColors.LightGreen1);
        range.getStyle().setFillPattern(ExcelPatternType.Solid);
        range.getStyle().getBorders().getByBordersLineType(BordersLineType.EdgeBottom).setLineStyle(LineStyleType.Medium);

        //四則演算します。
        currentFormula = "=1/2+3*4";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //日付関数
        currentFormula = "=TODAY()";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);
        sheet.getCellRange(currentRow,2).getStyle().setNumberFormat("YYYY/MM/DD");

        //時刻関数
        currentFormula = "=NOW()";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);
        sheet.getCellRange(currentRow,2).getStyle().setNumberFormat("H:MM AM/PM");

        //IF関数
        currentFormula = "=IF(B1=5,\"Yes\",\"No\")";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //PI関数
        currentFormula = "=PI()";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //三角関数
        currentFormula = "=SIN(PI()/6)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //count関数
        currentFormula = "=Count(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //max関数
        currentFormula = "=MAX(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //average関数
        currentFormula = "=AVERAGE(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //Sum関数
        currentFormula = "=SUM(B1:F1)";
        sheet.getCellRange(++currentRow,1).setText(currentFormula);
        sheet.getCellRange(currentRow,2).setFormula(currentFormula);

        //保存します。
        workbook.saveToFile("InsertFormulas.xlsx",FileFormat.Version2013);
    }
}

実行結果

f:id:lendoris:20210818121720p:plain

数式を読み取る

import com.spire.xls.*;

public class ReadFormulas {

    public static void main(String[] args) {

        //Workbook オブジェクトを作成します。
        Workbook workbook = new Workbook();

        //Excelファイルをロードします。
        workbook.loadFromFile("テスト.xlsx");

        //一つ目のシートを取得します。
        Worksheet sheet = workbook.getWorksheets().get(0);

        //B1からB13までのセルをロープします。
        for (Object cell: sheet.getCellRange("B1:B13")
        ) {
            CellRange cellRange = (CellRange)cell;

            //セルには数式があるかどうかを判定します。
            if (cellRange.hasFormula()){

                //出力します。
                String certainCell = String.format("セル[%d, %d]に数式あり:",cellRange.getRow(),cellRange.getColumn());
                System.out.println(certainCell + cellRange.getFormula());
            }
        }
    }

実行結果

 f:id:lendoris:20210818121727p:plain

 

 

 

 

 

 

 

 

0
0
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
0
0