数式と関数は、データの加工や分析を簡単かつ効率的に行うことができるExcelの2つの計算ツールです。数式は計算の記述であり、関数はあらかじめ定義された数式である。ユーザーは定数や演算子、他のセルから参照した値、さらには関数を使って数式を作成したり、関数を直接参照したりして、セル内で計算することができます。この記事では、無料のFree Spire.XLS for Javaを使用して、Excelワークブックに数式や関数を挿入したり、読み込んだりする方法を紹介します。
【依存関係の追加】
この方法は、無料のFree Spire.XLS for Javaが必要ですので、先にjarファイルをインポートしてください。
1. Maven
Maven を使用している場合、プロジェクトの pom.xml ファイルに以下のコードを追加することで、簡単にアプリケーションに JAR ファイルをインポートすることができます。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
2. 公式サイトよりJarファイルをダウンロード
まず、Free Spire.XLS for Javaの公式サイトよりzipファイルをダウンロードします。zipファイルを解凍し、libフォルダの下にあるSpire.Xls.jarファイルを依存関係としてプロジェクトにインポートしてください。
数式と関数の挿入
Free Spire.XLS for .NETが提供する Worksheet.getCellRange().setFormula() メソッド ワークシートに数式や関数を挿入する詳しい手順は、以下の通りです。
- Workbook のオブジェクトを作成します。
- Workbook.getWorksheets().get() メソッドを使用して、最初のワークシートを取得します。
- セルにデータを書き込み、セルの書式を設定します。
- Worksheet.getCellRange().setFormula() メソッドを使用して、特定のセルに数式や関数を追加します。
- Workbook.saveToFile() メソッドを使用して、ワークブックを保存します。
Java
import com.spire.xls.*;
public class insertFormulas {
public static void main(String[] args) {
//Workbookのオブジェクトを作成する
Workbook workbook = new Workbook();
//最初のワークシートを取得する
Worksheet sheet = workbook.getWorksheets().get(0);
//currentRow、currentFormulaの2つの変数を宣言する
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);
//計数関数
currentFormula = "=Count(B1:F1)";
sheet.getCellRange(++currentRow,1).setText("'"+currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//最大値関数
currentFormula = "=MAX(B1:F1)";
sheet.getCellRange(++currentRow,1).setText("'"+currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//平均関数
currentFormula = "=AVERAGE(B1:F1)";
sheet.getCellRange(++currentRow,1).setText("'"+currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//和算関数
currentFormula = "=SUM(B1:F1)";
sheet.getCellRange(++currentRow,1).setText("'"+currentFormula);
sheet.getCellRange(currentRow,2).setFormula(currentFormula);
//ワークブックを保存する
workbook.saveToFile("数式の挿入.xlsx",FileFormat.Version2013);
}
}
数式や関数の読み取り
CellRange.hasFormula() メソッドでセル内に数式があるかどうかをチェックし、CellRange.getFormula() メソッドで数式を取得することができます。以下はその詳細な手順です。
- Workbook のオブジェクトを作成します。
- Workbook.loadFromFile() メソッドを使用して Excel ワークブックを読み込みます。
- Workbook.getWorksheets().get() メソッドを使用してワークシートを取得します。
- ワークシート内のセルをループ処理します。
- CellRange.hasFormula() メソッドを使用して、セルに数式が含まれているかどうかを検出します。数式が含まれている場合は、CellRange.getFormula() メソッドを使用して数式を取得し、それを出力します。
- Workbook.saveToFile() メソッドを使用して、ワークブックを保存します。
Java
import com.spire.xls.CellRange;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
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());
}
}
}
}
数式や関数は、Excelでデータを処理するのに適しています。また、Excelには、図表やグラフなど、データを扱うのに役立つ他のツールもあります。エクセルデータの処理方法について詳しく知りたい方は、Spire.XLS for Javaチュートリアルをご覧ください。