※このページではアフィリエイト広告を紹介しています

JACOBでExcel / Word / PowerPointのファイルを開く

Java小ネタ
この記事は約7分で読めます。
広告

この記事の目的

JavaでOfficeファイルを操作するライブラリにJACOB(Java COM Bridge)があります。PythonのライブラリPyWin32と使い方が似ているため、PyWin32の記事と同じ章構成で使い方を紹介します。

GitHub - freemansoft/jacob-project: JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java. It uses JNI to make native calls to the COM libraries. JACOB runs on x86 and x64 environments supporting 32 bit and 64 bit JVMs. This repository was migrated from Sourceforge 2020/09
JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java. It uses JNI to make native calls...
PyWin32でExcel / Word / PowerPointのファイルを開く
想定する読者 ライブラリPyWin32に対して、どのメソッドを使ってExcel / Word / PowerPointのファイルを操作すれば良いかの取っ掛かりが難しいと感じている方 JavaでOfficeファイルを開く方法を下記記事で紹介し...
本記事で使用するJACOBのバージョンは、2020年9月リリースの1.14.3です。

各ファイルの開き方

概要

ライブラリJACOBを使ってExcel / Word / PowerPointのファイルを開く手順の概要は下記です。

  1. 同梱されているDLL(jacob-1.14.3-x64.dll)を適当な場所にコピーし、コピーしたフォルダのフォルダパスを環境変数Pathに追加します。筆者のPCが64bitのため、64bit用のDLL(x64)を使用します。
  2. ActiveXComponentクラスのコンストラクタで、対応するOfficeのアプリケーションを開きます。
  3. 1.で開いたアプリケーションを使って、Officeファイルを開きます。どんなメソッドが使えるかは「Office VBAリファレンス」を見ると分かります。
Office Visual Basic for Applications (VBA) リファレンス
この記事の内容は、VBA について学ぶことを希望したり、Office をカスタマイズする作業にプログラミングを役立てる方法について詳しく知ることを望む、経験ある Office ユーザーのために書かれています。
プログラムのビルドや実行はJava 8で行ってください。Java 11を使うと実行時にエラーが発生してしまいます。

Excelファイル

  1. new ActiveXComponent(“Excel.Application”)を呼び、Excelアプリケーションを開きます。
  2. ExcelアプリケーションのWorkbooksプロパティを取得します。
  3. WorkbooksプロパティのOpenメソッドでExcelファイルを開きます。
ActiveXComponent application = new ActiveXComponent("Excel.Application");
Dispatch.put(application, "Visible", true);

Dispatch document = null;
try {
	Dispatch workbooks =
			Dispatch.get(application, "Workbooks").getDispatch();
	document =
			Dispatch.call(workbooks, "Open", "[ファイル名]").getDispatch();
	// 何か処理を書く
} finally {
	if (document != null) {
		Dispatch.call(document, "Close");
	}
	Dispatch.call(application, "Quit");
}
Application.Workbooks プロパティ (Excel)
Office VBA リファレンス トピック
Workbooks.Open メソッド (Excel)
Office VBA リファレンス トピック
2行目の「Dispatch.put(application, “Visible”, true);」を削除すると、プログラム実行中のExcelの起動が画面上で見えなくなります。

Wordファイル

  1. new ActiveXComponent(“Word.Application”)を呼び、Wordアプリケーションを開きます。
  2. WordアプリケーションのDocumentsプロパティを取得します。
  3. DocumentsプロパティのOpenメソッドでWordファイルを開きます。
ActiveXComponent application = new ActiveXComponent("Word.Application");
Dispatch.put(application, "Visible", true);

Dispatch document = null;
try {
	Dispatch documents = 
			Dispatch.get(application, "Documents").getDispatch();
	document = 
			Dispatch.call(documents, "Open", "[ファイル名]").getDispatch();
	// 何か処理を書く
} finally {
	if (document != null) {
		Dispatch.call(document, "Close");
	}
	Dispatch.call(application, "Quit");
}
Application.Documents プロパティ (Word)
Office VBA リファレンス トピック
Documents.Open メソッド (Word)
Office VBA リファレンス トピック
2行目の「Dispatch.put(application, “Visible”, true);」を削除すると、プログラム実行中のWordの起動が画面上で見えなくなります。

PowerPointファイル

  1. new ActiveXComponent(“PowerPoint.Application”)を呼び、PowerPointアプリケーションを開きます。
  2. PowerPointアプリケーションのPresentationsプロパティを取得します。
  3. PresentationsプロパティのOpenメソッドでPowerPointファイルを開きます。
ActiveXComponent application = new ActiveXComponent("PowerPoint.Application");
Dispatch.put(application, "Visible", true);

Dispatch document = null;
try {
	Dispatch presentations =
			Dispatch.get(application, "Presentations").getDispatch();
	document =
			Dispatch.call(presentations, "Open", "[ファイル名]").getDispatch();
	// 何か処理を書く
} finally {
	if (document != null) {
		Dispatch.call(document, "Close");
	}
	Dispatch.call(application, "Quit");
}
Application.Presentations プロパティ (PowerPoint)
Office VBA リファレンス トピック
Presentations.Open メソッド (PowerPoint)
Office VBA リファレンス トピック
2行目の「Dispatch.put(application, “Visible”, true);」を削除しても、プログラム実行中のPowerPointの起動は画面上で見えるままです。一方、「Dispatch.put(application, “Visible”, false);」にするとエラーになります。

コメント

タイトルとURLをコピーしました