-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMailMergeWordDocument.java
42 lines (37 loc) · 1.31 KB
/
MailMergeWordDocument.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package mailmerge;
import java.io.File;
import com.syncfusion.docio.*;
public class MailMergeWordDocument {
public static void main(String[] args) throws Exception {
// Creates a WordDocument instance
WordDocument document = new WordDocument();
String basePath = getDataDir("LetterTemplate.docx");
// Opens the template Word document.
document.open(basePath, FormatType.Docx);
String[] fieldNames = { "ContactName", "CompanyName", "Address", "City", "Country", "Phone" };
String[] fieldValues = { "Nancy Davolio", "Syncfusion", "507 - 20th Ave. E.Apt. 2A", "Seattle, WA", "USA",
"(206) 555-9857-x5467" };
// Performs mail merge
document.getMailMerge().execute(fieldNames, fieldValues);
// Saves the Word document
document.save("Result.docx");
// Closes the Word document
document.close();
System.out.println("Document generated successfully");
}
/**
* Get the file path
*
* @param path specifies the file path
*/
private static String getDataDir(String path) {
File dir = new File(System.getProperty("user.dir"));
if (!(dir.toString().endsWith("Java-Mail-Merge-Examples")))
dir = dir.getParentFile();
dir = new File(dir, "resources");
dir = new File(dir, path);
if (dir.isDirectory() == false)
dir.mkdir();
return dir.toString();
}
}