Слияние почты в документе Word – очень мощная функция, позволяющая пользователям писать и отправлять персонализированные письма или электронные письма многим разным людям одновременно. В этой статье будет продемонстрировано, как создать шаблон слияния почты, а затем объединить текстовое значение в шаблон с помощью бесплатного Spire. Документ для Java.
Зависимость от импорта Jar (2 метода) ● Скачайте бесплатно Шпиль. Документ для Java и распакуйте его, затем добавьте Spire.Doc.jar файл в ваше Java-приложение в качестве зависимости.
● Непосредственно добавьте зависимость jar в проект maven, добавив следующие конфигурации в pom.xml .
com.e-iceblue e-iceblue http://repo.e-iceblue.com/nexus/content/groups/public/ e-iceblue spire.doc.free 3.9.0
Пример Кода
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import java.text.SimpleDateFormat; import java.util.Date; public class MailMerge { public static void main(String[] args) throws Exception { String output = "mailMerge.docx"; //Create a Document instance Document document = new Document(); //Add a section Section section = document.addSection(); //Add 3 paragraphs to the section Paragraph para = section.addParagraph(); Paragraph para2 = section.addParagraph(); Paragraph para3 = section.addParagraph(); //Add mail merge templates to each paragraph para.setText("Contact Name: "); para.appendField("Contact Name", FieldType.Field_Merge_Field); para2.setText("Phone: "); para2.appendField("Phone", FieldType.Field_Merge_Field); para3.setText("Date: "); para3.appendField("Date", FieldType.Field_Merge_Field); //Set the value for the mail merge template by the field name Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(currentTime); String[] filedNames = new String[]{"Contact Name", "Phone", "Date"}; String[] filedValues = new String[]{"Robert Chan", "+1 (69) 123456", dateString}; //Merge the specified value into template document.getMailMerge().execute(filedNames, filedValues); //save the document to file document.saveToFile(output, FileFormat.Docx); } }
Снимок итогового документа
Оригинал: “https://dev.to/jazzzzz/java-create-mail-merge-and-merge-text-value-in-word-486m”