Mail Merge Reporting

Word 文档中需要配置

Aspose.words 自定义 WordTemplate 生成新文档 - 图1

代码示例

  1. public class ExecuteSimpleMailMerge {
  2. private static final String dataDir = Utils.getSharedDataDir(ExecuteSimpleMailMerge.class) + "MailMerge/";
  3. public static void main(String[] args) throws Exception {
  4. //ExStart:
  5. // Open an existing document.
  6. Document doc = new Document(dataDir + "MailMerge.ExecuteArray.doc");
  7. // Trim trailing and leading whitespaces mail merge values
  8. doc.getMailMerge().setTrimWhitespaces(false);
  9. // Fill the fields in the document with user data.
  10. doc.getMailMerge().execute(new String[] { "FullName", "Company", "Address", "Address2", "City" },
  11. new Object[] { "James Bond", "MI5 Headquarters", "Milbank", "", "London" });
  12. doc.save(dataDir + "MailMerge.ExecuteArray_Out.doc");
  13. //ExEnd:
  14. }
  15. }

Linq Reporting Engine

Word Template 语法

<<[UserName]>>
<<foreach [m in Data]>><<[m.UserName]>><</foreach>>

不需要在Word文档中配置域

代码示例

  1. public static void main(String[] args) throws Exception
  2. {
  3. //ExStart:HelloWorld
  4. // The path to the documents directory.
  5. String dataDir = Utils.getDataDir(HelloWorld.class);
  6. String fileName = "HelloWorld.doc";
  7. // Load the template document.
  8. Document doc = new Document(dataDir + fileName);
  9. // Create an instance of sender class to set it's properties.
  10. Sender sender = new Sender();
  11. sender.setName("LINQ Reporting Engine");
  12. sender.setMessage("Hello World");
  13. // Create a Reporting Engine.
  14. ReportingEngine engine = new ReportingEngine();
  15. // Execute the build report.
  16. engine.buildReport(doc, sender, "sender");
  17. dataDir = dataDir + Utils.GetOutputFilePath(fileName);
  18. // Save the finished document to disk.
  19. doc.save(dataDir);
  20. //ExEnd:HelloWorld
  21. System.out.println("\nTemplate document is populated with the data about the sender.\nFile saved at " + dataDir);
  22. }

参考资源

Aspose.Words doc - LINQ Reporting Engine