Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ You can download the jars from the Syncfusion [maven repository](https://jars.sy

- [Copy table styles in Java](copytablestyles/) - Copy table styles from one Word document to another.

- [Fill data in Word Table dynamically in Java](dynamictable/) - Create dynamic table in Word document.

- [Fill data in Word Table dynamically using mail merge in Java](dynamictableusingmailmerge/) -Create dynamic table using mail merge in Word document.

# Screenshots

**Built-in table style**
Expand Down
151 changes: 151 additions & 0 deletions dynamictable/DynamicTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import java.io.*;
import com.syncfusion.docio.*;
import com.syncfusion.javahelper.system.*;
import com.syncfusion.javahelper.system.collections.generic.ListSupport;
import com.syncfusion.javahelper.system.io.*;
import com.syncfusion.javahelper.system.xml.*;

public class DynamicTable {
public static void main(String[] args) throws Exception {
// Loads the template document.
WordDocument document = new WordDocument(getDataDir("DynamicTable_Template.docx"));
// Creates a list of employee details.
ListSupport<Employees> employeeDetails = getEmployeeDetails();
// Iterates each item in the list.
for (Object employee_tempObj : employeeDetails)
{
Employees employee = (Employees) employee_tempObj;
// Accesses the table in the document.
IWTable table = document.getSections().get(0).getTables().get(0);
// Initializes the paragraph.
IWParagraph paragraph = null;
// Initializes and add new row to the table.
WTableRow newRow = null;
newRow = table.addRow();
// Gets the employee photo and convert that base64 string to bytes.
byte[] bytes = ConvertSupport.fromBase64String(employee.getPhoto());
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
int i = table.getRows().getCount();
// Appends the picture to the first cell.
table.getRows().get(i - 1).getCells().get(0).addParagraph().appendPicture(stream);
// Appends the employee details in the second cell.
paragraph = table.getRows().get(i - 1).getCells().get(1).addParagraph();
paragraph.appendText(employee.getName() + "\n" + employee.getTitle() + "\n" + employee.getAddress() + "\n"
+ employee.getHomePhone());
}
// Saves and closes the document.
document.save("Result.docx");
document.close();
}

/**
*
* Gets the list of employee details.
*
*/
private static ListSupport<Employees> getEmployeeDetails() throws Exception {
// Gets list of employee details.
ListSupport<Employees> employees = new ListSupport<Employees>(Employees.class);
// Reads the xml document.
FileStreamSupport fs = new FileStreamSupport(getDataDir("EmployeesList.xml"), FileMode.Open, FileAccess.Read);
XmlReaderSupport reader = XmlReaderSupport.create(fs);
if (reader == null)
throw new Exception("reader");
while (reader.getNodeType() != XmlNodeType.Element)
reader.read();
if (reader.getLocalName() != "Employees")
throw new Exception(StringSupport.concat("Unexpected xml tag ", reader.getLocalName()));
reader.read();
while (reader.getNodeType() == XmlNodeType.Whitespace)
reader.read();
// Iterates to add the employee details in list.
while (reader.getLocalName() != "Employees")
{
if (reader.getNodeType() == XmlNodeType.Element)
{
switch (reader.getLocalName())
{
case "Employee":
employees.add(getEmployees(reader));
break;
}
}
else
{
reader.read();
if ((reader.getLocalName() == "Employees") && reader.getNodeType() == XmlNodeType.EndElement)
break;
}
}
return employees;
}

/**
*
* Gets the employees.
*
* @param reader Syncfusion's XML reader to read the XML files..
*/
private static Employees getEmployees(XmlReaderSupport reader) throws Exception {
if (reader == null)
throw new Exception("reader");
while (reader.getNodeType() != XmlNodeType.Element)
reader.read();
if (reader.getLocalName() != "Employee")
throw new Exception(StringSupport.concat("Unexpected xml tag ", reader.getLocalName()));
reader.read();
while (reader.getNodeType() == XmlNodeType.Whitespace)
reader.read();
Employees employee = new Employees();
while (reader.getLocalName() != "Employee")
{
if (reader.getNodeType() == XmlNodeType.Element)
{
switch (reader.getLocalName())
{
case "Name":
employee.setName(reader.readContentAsString());
break;
case "Title":
employee.setTitle(reader.readContentAsString());
break;
case "Address":
employee.setAddress(reader.readContentAsString());
break;
case "HomePhone":
employee.setHomePhone(reader.readContentAsString());
break;
case "Photo":
employee.setPhoto(reader.readContentAsString());
break;
default:
reader.skip();
break;
}
}
else
{
reader.read();
if ((reader.getLocalName() == "Employee") && reader.getNodeType() == XmlNodeType.EndElement)
break;
}
}
return employee;
}

/**
* 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-Word-Table-Examples")))
dir = dir.getParentFile();
dir = new File(dir, "resources");
dir = new File(dir, path);
if (dir.isDirectory() == false)
dir.mkdir();
return dir.toString();
}
}
76 changes: 76 additions & 0 deletions dynamictable/Employees.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
public class Employees
{
private String m_name;
private String m_title;
private String m_address;
private String m_homePhone;
private String m_photo;
// Gets the employee name.
public String getName() throws Exception {
return m_name;
}
// Sets the employee name.
public String setName(String value) throws Exception {
m_name = value;
return value;
}
// Gets the designation of the employee.
public String getTitle() throws Exception {
return m_title;
}
// Sets the designation of the employee.
public String setTitle(String value) throws Exception {
m_title = value;
return value;
}
// Gets the address of the employee.
public String getAddress() throws Exception {
return m_address;
}
// Sets the address of the employee.
public String setAddress(String value) throws Exception {
m_address = value;
return value;
}
// Gets the contact number of the employee.
public String getHomePhone() throws Exception {
return m_homePhone;
}
// Sets the contact number of the employee.
public String setHomePhone(String value) throws Exception {
m_homePhone = value;
return value;
}
// Gets the photo of the employee.
public String getPhoto() throws Exception {
return m_photo;
}
// Sets the photo of the employee.
public String setPhoto(String value) throws Exception {
m_photo = value;
return value;
}
/**
* Initializes a new instance of the Employees class with the specified name,
* title, address, contact number and photo.
*
* @param name Name of the employee.
* @param title Designation of the employee.
* @param address Address of the employee.
* @param homePhone Contact number of the employee.
* @param photo Photo of the employee.
* @throws Exception
*/
public Employees(String name, String title, String address, String homePhone, String photo) throws Exception {
m_name = name;
m_title = title;
m_address = address;
m_homePhone = homePhone;
m_photo = photo;
}
/**
* Initializes a new instance of the Employees class.
*/
public Employees() throws Exception {
}
}
137 changes: 137 additions & 0 deletions dynamictableusingmailmerge/DynamicTableUsingMailmerge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import java.io.File;
import com.syncfusion.docio.*;
import com.syncfusion.javahelper.system.collections.generic.ListSupport;
import com.syncfusion.javahelper.system.io.*;
import com.syncfusion.javahelper.system.xml.*;

public class DynamicTableUsingMailmerge {

public static void main(String[] args) throws Exception {
// Loads the template document.
WordDocument document = new WordDocument(getDataDir("Template.docx"), FormatType.Docx);
// Creates MailMergeDataTable.
MailMergeDataTable mailMergeDataTableStock = getMailMergeDataTableStock();
// Executes Mail Merge with group.
document.getMailMerge().executeGroup(mailMergeDataTableStock);
// Saves and closes the document.
document.save("Result.docx", FormatType.Docx);
document.close();
}

/**
*
* Gets the mail merge data table.
*
*/
private static MailMergeDataTable getMailMergeDataTableStock() throws Exception {
// Gets list of stock details.
ListSupport<StockDetails> stockDetails = new ListSupport<StockDetails>(StockDetails.class);
FileStreamSupport fs = new FileStreamSupport(getDataDir("StockDetails.xml"), FileMode.Open, FileAccess.Read);
// Reads the xml document.
XmlReaderSupport reader = XmlReaderSupport.create(fs);
if (reader == null)
throw new Exception("reader");
while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue())
reader.read();
if (!(reader.getLocalName() == "StockMarket"))
throw new Exception("Unexpected xml tag " + reader.getLocalName());
reader.read();
while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue())
reader.read();
// Iterates to add the stock details in list.
while (!(reader.getLocalName() == "StockMarket"))
{
if (reader.getNodeType().getEnumValue() == XmlNodeType.Element.getEnumValue())
{
switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName()))
{
case "StockDetails":
stockDetails.add(getStockDetails(reader));
break;
}
}
else
{
reader.read();
if ((reader.getLocalName() == "StockMarket")
&& reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue())
break;
}
}
// Creates an instance of MailMergeDataTable by specifying mail merge group name and IEnumerable collection.
MailMergeDataTable dataTable = new MailMergeDataTable("StockDetails", stockDetails);
reader.close();
fs.close();
return dataTable;
}

/**
*
* Gets the StockDetails.
*
* @param reader Syncfusion's XML reader to read the XML files.
*/
private static StockDetails getStockDetails(XmlReaderSupport reader) throws Exception {
if (reader == null)
throw new Exception("reader");
while (reader.getNodeType().getEnumValue() != XmlNodeType.Element.getEnumValue())
reader.read();
if (!(reader.getLocalName() == "StockDetails"))
throw new Exception("Unexpected xml tag " + reader.getLocalName());
reader.read();
while (reader.getNodeType().getEnumValue() == XmlNodeType.Whitespace.getEnumValue())
reader.read();
StockDetails stockDetails = new StockDetails();
while (!(reader.getLocalName() == "StockDetails"))
{
if (reader.getNodeType().getEnumValue() == XmlNodeType.Element.getEnumValue())
{
switch ((reader.getLocalName()) == null ? "string_null_value" : (reader.getLocalName()))
{
case "TradeNo":
stockDetails.setTradeNo(reader.readContentAsString());
break;
case "CompanyName":
stockDetails.setCompanyName(reader.readContentAsString());
break;
case "CostPrice":
stockDetails.setCostPrice(reader.readContentAsString());
break;
case "SharesCount":
stockDetails.setSharesCount(reader.readContentAsString());
break;
case "SalesPrice":
stockDetails.setSalesPrice(reader.readContentAsString());
break;
default:
reader.skip();
break;
}
}
else
{
reader.read();
if ((reader.getLocalName() == "StockDetails")
&& reader.getNodeType().getEnumValue() == XmlNodeType.EndElement.getEnumValue())
break;
}
}
return stockDetails;
}

/**
* 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-Word-Table-Examples")))
dir = dir.getParentFile();
dir = new File(dir, "resources");
dir = new File(dir, path);
if (dir.isDirectory() == false)
dir.mkdir();
return dir.toString();
}
}
Loading