Wordで変数を追加/カウント/取得/削除する
この記事では、JavaでのWord文書内の変数を追加、カウント、取得、削除する方法について説明します.
自由APIのJAR依存をJavaアプリケーションにインポートする
● 無料のAPI(Free Spire.Doc for Java)をダウンロードして、それを解凍し、その後、尖塔を追加します.doc.Javaファイルに依存するjarファイル.
● 次の構成をPOMに追加することで、MavenプロジェクトにJAR依存を直接追加します.XML
“Temp”という名前のドキュメント変数をWord文書に12の値で追加します.
変数3の数を数えます
パラメータ4 nameは変数の名前と値を取得する
eval 5 eval特定の変数を削除する
自由APIのJAR依存をJavaアプリケーションにインポートする
● 無料のAPI(Free Spire.Doc for Java)をダウンロードして、それを解凍し、その後、尖塔を追加します.doc.Javaファイルに依存するjarファイル.
● 次の構成をPOMに追加することで、MavenプロジェクトにJAR依存を直接追加します.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.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
eval 2 yield変数を追加する“Temp”という名前のドキュメント変数をWord文書に12の値で追加します.
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
public class AddVariables {
public static void main(String[] args){
//Create a Document instance
Document document = new Document();
//Add a section
Section section = document.addSection();
//Add a paragraph to the section
Paragraph paragraph = section.addParagraph();
//Add a DocVariable field to the paragraph
paragraph.appendField("Temp", FieldType.Field_Doc_Variable);
//Add a document variable to the DocVariable field
document.getVariables().add("Temp", "12");
//Update fields in the document
document.isUpdateFields(true);
//Save the result document
document.saveToFile("AddVariables.docx", FileFormat.Docx_2013);
}
}
変数3の数を数えます
import com.spire.doc.Document;
public class CountVariables {
public static void main(String[] args){
//Load the Word document
Document document = new Document();
document.loadFromFile("AddVariables.docx");
//Get the number of variables in the document
int number = document.getVariables().getCount();
StringBuilder content = new StringBuilder();
content.append("The number of variables is: " + number);
System.out.println(content.toString());
}
}
パラメータ4 nameは変数の名前と値を取得する
import com.spire.doc.Document;
public class RetrieveVariables {
public static void main(String[] args){
//Load the Word document
Document document = new Document();
document.loadFromFile("AddVariables.docx");
//Retrieve the name of a variable by index
String s1 = document.getVariables().getNameByIndex(0);
//Retrieve the value of a variable by index
String s2 = document.getVariables().getValueByIndex(0);
//Retrieve the value of a variable by name
String s3 = document.getVariables().get("Temp");
System.out.println("The name of the variable retrieved by index 0 is: " + s1);
System.out.println("The value of the variable retrieved by index 0 is: " + s2);
System.out.println("The value of the variable retrieved by name \"Temp\" is: " + s3);
}
}
eval 5 eval特定の変数を削除する
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class RemoveVariables {
public static void main(String[] args){
//Load the Word document
Document document = new Document();
document.loadFromFile("AddVariables.docx");
//Remove a variable by name
document.getVariables().remove("Temp");
//Update fields in the document
document.isUpdateFields (true);
//Save the result document
document.saveToFile("RemoveVariables.docx", FileFormat.Docx_2013);
}
}
Reference
この問題について(Wordで変数を追加/カウント/取得/削除する), 我々は、より多くの情報をここで見つけました https://dev.to/codesharing/add-count-retrieve-remove-variables-in-word-using-java-3o15テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol