메이븐으로 설치
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>12.2.2</version>
</dependency>
관련된 링크들
Word 문서의 변수는 텍스트 교체, 삭제 등 편리하고 정확한 텍스트 관리 기능이 특징인 필드 유형입니다. 찾기 및 바꾸기 기능과 비교하여 변수에 값을 할당하여 텍스트를 바꾸는 것이 더 빠르고 오류 발생 가능성이 적습니다. 이 기사에서는 방법을 보여 드리겠습니다 Word 문서에서 변수 추가 또는 변경 Spire.Doc for Java 사용하여 프로그래밍 방식으로.
Spire.Doc for Java 설치
먼저 Spire.Doc.jar 파일을 Java 프로그램의 종속성으로 추가해야 합니다. JAR 파일은 이 링크에서 다운로드할 수 있습니다. 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.doc</artifactId>
<version>13.11.2</version>
</dependency>
</dependencies>
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) {
//Create an object of Document
Document document = new Document();
//Add a section
Section section = document.addSection();
//Add a paragraph
Paragraph paragraph = section.addParagraph();
//Set text format
CharacterFormat characterFormat = paragraph.getStyle().getCharacterFormat();
characterFormat.setFontName("Times New Roman");
characterFormat.setFontSize(14);
//Set the page margin
section.getPageSetup().getMargins().setTop(80f);
//Add variable fields to the paragraph
paragraph.appendField("Term", FieldType.Field_Doc_Variable);
paragraph.appendText(" is an object.\r\n");
paragraph.appendField("Term", FieldType.Field_Doc_Variable);
paragraph.appendText(" is not a backdrop, an illusion, or an emergent phenomenon.\r\n");
paragraph.appendField("Term", FieldType.Field_Doc_Variable);
paragraph.appendText(" has a physical size that be measured in laboratories.");
//Get the variable collection
VariableCollection variableCollection = document.getVariables();
//Assign a value to the variable
variableCollection.add("Term", "Time");
//Update the fields in the document
document.isUpdateFields(true);
//Save the document
document.saveToFile("AddVariables.docx", FileFormat.Auto);
document.dispose();
}
}

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) {
//Create an object of Document
Document document = new Document();
//Load a Word document
document.loadFromFile("AddVariables.docx");
//Get the variable collection
VariableCollection variableCollection = document.getVariables();
//Assign a new value to a variable
variableCollection.set("Term", "The time");
//Update the fields in the document
document.isUpdateFields(true);
//Save the document
document.saveToFile("ChangeVariable.docx", FileFormat.Auto);
document.dispose();
}
}

임시 라이센스 신청
생성된 문서에서 평가 메시지를 제거하고 싶거나, 기능 제한을 없애고 싶다면 30일 평가판 라이센스 요청 자신을 위해.