i have the word api for java. Im looking for examples of creating a merge document using a CSV file. Can someone point me to examples?
Document doc = new Document(MyDir + "in.docx");
doc.getMailMergeSettings().clear();
String query = "SELECT * FROM 'csvdata'";
MailMergeSettings mms = doc.getMailMergeSettings();
mms.setMainDocumentType(MailMergeMainDocumentType.MAILING_LABELS);
mms.setDataType(MailMergeDataType.NATIVE);
mms.setDataSource("C:\\Temp\\csvdata.csv");
mms.setQuery(query);
mms.setLinkToQuery(false);
mms.setViewMergedData(true);
doc.save(MyDir + "Output.docx");
i have attached a word document and a csv file that i use. Basically, i want to use the csv as the datasource to populate the word document merge fields. I dont need any sql selection since the word document has criteria in it to handle that.
i know this is probably an easy solution. just new to java and your great api.
Hi,
Could you provide a simple sample of code. New to java here
DataTable dataTable = new DataTable();
Boolean header = true;
org.apache.commons.csv.CSVParser csvReader = new org.apache.commons.csv.CSVParser(new FileReader(MyDir + "test.csv"));
String[] content;
while((content = csvReader.getLine()) != null)
{
if(header)
{
for(int i=0; i < content.length; i++)
{
dataTable.getColumns().add(content[i]);
}
header = false;
}
else
{
DataRow dataRow = dataTable.newRow();
dataRow.setItemArray(content);
dataTable.getRows().add(dataRow);
}
}
Document doc = new Document(MyDir + "test.doc");
doc.getMailMerge().execute(dataTable);
doc.save(MyDir + "Out v16.10.0.docx");