Hi all,
First I want to thank you of the Aspose documentation, it has helped me a lot.
I am new in developping using aspose.word API.
I have read the documentation precisely in section How to Execute Mail Merge with Regions.
But the mail merge didn’t work and I didn’t figure out the problem.
I want to execute the mail merge with region and generate the result document.
I have prepared the template “startTable.docx” document and I have defined the region “Orders”.
Simply I want to retreive the sender information from the database and execute the mail merge with region.
So the expected result is that the region with name “orders” will output the sender information that
was retrieved from the database.
But the document “startTableResult.docx” dosen’t output the result in place of merged field and I didn’t figure out the problem.
I am using a spring boot application, and I add the aspose.word dependency in the pom file.
This is the main class
@SpringBootApplication
public class DinnerInvitaionDemoApplication implements CommandLineRunner {
static final String USER = "root";
static final String PASS = "";
@Autowired
private InvitedRepository invitedRepository;
@Autowired
private SenderRepository senderRepository;
@Autowired
private OrderDetailRepository orderDetailRepository;
@Autowired
private SenderService senderService;
public static void main(String[] args) {
SpringApplication.run(DinnerInvitaionDemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String dataDir = "src/main/resources/templates/";
Document document = new Document(dataDir + "TableStart.docx");
DataTable senderTable = getSenderInformation();
document.getMailMerge().executeWithRegions(senderTable);
DataTable orderDetailTable = getOrderDetails();
document.getMailMerge().executeWithRegions(orderDetailTable);
document.save(dataDir + "TableStartResult.docx");
System.out.println("Merge complete successfully !! ");
}
private DataTable getSenderInformation() throws Exception {
ResultSet resultSet = executeDataTable("SELECT * FROM SENDER WHERE ID = 1");
resultSet.next();
// just to make sure that I have retrieved the row from the database
System.out.println(resultSet.getString(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
return new DataTable(resultSet, "Orders");
}
private DataTable getOrderDetails() throws Exception {
ResultSet resultSet = executeDataTable("SELECT * FROM ORDER_DETAIL WHERE product_id = 2");
resultSet.next();
// just to make sure that I have retrieved the row from the database
System.out.println(resultSet.getString(1));
System.out.println(resultSet.getString(2));
return new DataTable(resultSet, "OrderDetail");
}
private ResultSet executeDataTable(String format) throws Exception {
String connString = "jdbc:mysql://localhost:3306/dinnerdb";
Properties props = new java.util.Properties();
props.put("charSet", "Cp1252");
Connection conn = DriverManager.getConnection(connString, USER, PASS);
// DriverManager.getConnectio
Statement statement = conn.createStatement();
return statement.executeQuery(format);
}
}
Thanks
This the Sender model:
@Entity
public class Sender {
@Id
@GeneratedValue
private Long id;
@Column(name = "senderName")
private String senderName;
@Column(name = "firstAdress")
private String firstAdress;
@Column(name = "secondAdress")
private String secondAdress;
public Sender() {
}
public Sender(String senderName, String firstAdress, String secondAdress) {
super();
this.senderName = senderName;
this.firstAdress = firstAdress;
this.secondAdress = secondAdress;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSenderName() {
return senderName;
}
public void setSenderName(String senderName) {
this.senderName = senderName;
}
public String getFirstAdress() {
return firstAdress;
}
public void setFirstAdress(String firstAdress) {
this.firstAdress = firstAdress;
}
public String getSecondAdress() {
return secondAdress;
}
public void setSecondAdress(String secondAdress) {
this.secondAdress = secondAdress;
}
@Override
public String toString() {
return "Sender [id=" + id + ", senderName=" + senderName + ", firstAdress=" + firstAdress + ", secondAdress="
+ secondAdress + "]";
}
}
this the orderDetail model:
@Entity
public class OrderDetail {
@Id
@GeneratedValue
private Long productId;
private String productName;
private Long quantity;
private Long unitPrice;
private Long discount;
private Long extendedPrice;
public OrderDetail() {
}
public OrderDetail(String productName, Long quantity, Long unitPrice, Long discount, Long extendedPrice) {
super();
this.productName = productName;
this.quantity = quantity;
this.unitPrice = unitPrice;
this.discount = discount;
this.extendedPrice = extendedPrice;
}
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Long getQuantity() {
return quantity;
}
public void setQuantity(Long quantity) {
this.quantity = quantity;
}
public Long getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(Long unitPrice) {
this.unitPrice = unitPrice;
}
public Long getDiscount() {
return discount;
}
public void setDiscount(Long discount) {
this.discount = discount;
}
public Long getExtendedPrice() {
return extendedPrice;
}
public void setExtendedPrice(Long extendedPrice) {
this.extendedPrice = extendedPrice;
}
@Override
public String toString() {
return "OrderDetail [productId=" + productId + ", productName=" + productName + ", quantity=" + quantity
+ ", unitPrice=" + unitPrice + ", discount=" + discount + ", extendedPrice=" + extendedPrice + "]";
}
}
I am using spring data (JPA/Hibernate) to manage the persistence layer:
the Sender table in Mysql database name “Sender”
senderTable.PNG (6.0 KB)
The order_detail table :
order_detail.PNG (10.0 KB)
The template “StartTable.docx” and the result “StartTableResult.docx” which hasn’t output the data.
templateAndResultDoc.zip (18.1 KB)
I need help and thanks.