0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaはExcelの数式を追加して読み取ります

Posted at

Excelテーブルではデータを計算や処理する場合、仕事の効率を上げるために、様々なExcel関数式を使うことがよくあります。この記事では、Free Spire.XLS for Javaを使用してExcelセルに数式を追加する方法と、セル内の数式を読み取る方法を紹介します。

JARパッケージのインポート
方法1: Free Spire.XLS for Javaをダウンロードして解凍したら、libフォルダーのSpire.Xls.jarパッケージを依存関係としてJavaアプリケーションにインポートします。

方法2: Mavenリポジトリから直接にJARパッケージをインストールしたら、pom.xmlファイルを次のように構成します。

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>2.2.0</version>
    </dependency>
</dependencies>

数式を追加する

import com.spire.xls.*;

public class InsertFormulas {

    public static void main(String[] args) {

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

        //最初目のワークシートを取得します
        Worksheet sheet = workbook.getWorksheets().get(0);

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

        //列の幅を設定します
        sheet.setColumnWidth(1, 26);
        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.jpg

数式を読み取る

import com.spire.xls.*;

public class ReadFormulas {

    public static void main(String[] args) {

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

        //Excelドキュメントをロードします
        workbook.loadFromFile("InsertFormulas.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());
            }
        }
    }
}

rf.jpg

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?