Word 文書における変数は、テキストの置換や削除など、便利で正確なテキスト管理ができることを特徴とするフィールドの一種です。検索・置換機能に比べ、変数に値を代入してテキストを置換する方が、高速でエラーも少ない。この記事では、Spire.Doc for Java を使用して Word 文書で変数を追加または変更する方法を示します。 Java で Word への変数を挿入する Java で Word で変数の値を変更する Spire.Doc for Java をインストールします まず、Spire.Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。 <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.doc</artifactId> <version>11.10.3</version> </dependency> </dependencies> Java で Word への変数を挿入する 変数は Word のフィールドの一種であるため、Paragraph.appendField(String fieldName, FieldType.Field_Doc_Variable) メソッドを使用して変数をWord文書に挿入し、VariableCollection.add() メソッドを使用して変数に値を割り当てることができます。なお、変数に値を割り当てた後は、文書のフィールドを更新して割り当てられた値を表示する必要があります。詳細な手順は以下の通りです。 Document のオブジェクトを作成します。 Document.addSection() メソッドを使用して、文書にセクションを追加します。 Section.addParagraph() メソッドを使用して、セクションに段落を追加します。 Paragraph.appendField(String fieldName, FieldType.Field_Doc_Variable) メソッドを使用して、段落に変数フィールドを追加します。 Document.getVariables() メソッドを使用して、変数コレクションを取得します。 VariableCollection.add() メソッドを使用して、変数に値を代入します。 Document.isUpdateFields() メソッドを使用して、文書内のフィールドを更新します。 Document.saveToFile() メソッドを使用して結果文書を保存します。 Java import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.formatting.CharacterFormat; public class AddVariables { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //セクションを追加する Section section = document.addSection(); //段落を追加する Paragraph paragraph = section.addParagraph(); //テキスト形式を設定する CharacterFormat characterFormat = paragraph.getStyle().getCharacterFormat(); characterFormat.setFontName("Yu Mincho"); characterFormat.setFontSize(12); //ページ余白を設定する section.getPageSetup().getMargins().setTop(80f); //変数フィールドを段落に追加する paragraph.appendField("Time", FieldType.Field_Doc_Variable); paragraph.appendText("は宝物であり、逃げる水のように貴重です。rn"); paragraph.appendField("Time", FieldType.Field_Doc_Variable); paragraph.appendText("は風のように過ぎ去り、砂のように滑ります。rn"); paragraph.appendField("Time", FieldType.Field_Doc_Variable); paragraph.appendText("は人生の舞台であり、私たちはその中で輝きます。"); //変数コレクションを取得する VariableCollection variableCollection = document.getVariables(); //変数に値を代入する variableCollection.add("Time", "時間"); //文書内のフィールドを更新する document.isUpdateFields(true); //結果文書を保存する document.saveToFile("AddVariables.docx", FileFormat.Auto); document.dispose(); } } Java で Word で変数の値を変更する Spire.Doc for Java では、VariableCollection.set() メソッドを使用して変数の値を変更することができます。また、文書内のフィールドを更新した後、変数のすべての出現箇所には新しく割り当てられた値が表示されるため、高速かつ正確なテキストの置換が実現されます。詳細な手順は以下の通りです。 Document のオブジェクトを作成します。 Document.loaFromFile() メソッドを使用して Word 文書をロードします。 Document.getVariables() メソッドを使用して変数コレクションを取得します。 VariableCollection.set() メソッドを使用して、名前で特定の変数に新しい値を代入します。 Document.isUpdateFields() メソッドを使用して、文書内のフィールドを更新します。 Document.saveToFile() メソッドを使用して結果文書を保存します。 Java import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.VariableCollection; public class ChangeVariableValue { public static void main(String[] args) { //Documentのオブジェクトを作成する Document document = new Document(); //Word文書をロードする document.loadFromFile("AddVariables.docx"); //変数コレクションを取得する VariableCollection variableCollection = document.getVariables(); //変数に値を代入する variableCollection.set("Time", "じかん"); //文書内のフィールドを更新する document.isUpdateFields(true); //結果文書を保存する document.saveToFile("ChangeVariable.docx", FileFormat.Auto); document.dispose(); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。