This is a small application which allows to repeatedly replace markers in a Microsoft Word document with items taken from a CSV/Microsoft Excel file.
I started this project as I was quite disappointed with the functionality that LibreOffice offers, I especially wanted something that is repeatable/automatable and does not produce spurious strange results and also does not need re-configuration each time the mail-merge is (re-)run.
All you need is a Word-Document in "docx" format (>= 2003) which acts as template and an Excel .xls/.xlsx or CSV file which contains one row for each time the template-document should be produced.
The word-document can contain template-markers (enclosed in
The first row of the first sheet of the Excel/CSV file is read as a header-row which is used to match the template-names used in the Word-template.
Only the first sheet of Excel files are read.
The result is a single merged Word-document which contains a replaced copy of the template for each line in the Excel file.
git clone https://github.com/centic9/poi-mail-merge.git
cd poi-mail-merge
./gradlew installDist
./run.sh <word-template> <excel/csv-file> <output-file>
There are some sample files in the directory samples
, you can run these
as follows
./gradlew installDist
build\install\poi-mail-merge\bin\poi-mail-merge.bat samples\Template.docx samples\Lines.xlsx build\Result.docx
on Unix you can use the following steps
./gradlew installDist
./run.sh samples/Template.docx samples/Lines.xlsx build/Result.docx
If you find this tool useful and would like to support work on it, you can Sponsor the author
You can use the tool unoconv
from OpenOffice/LibreOffice to further
convert the resulting docx, e.g. to PDF:
unoconv -vvv --timeout=60 --doctype=document --output=result.pdf result.docx
For XLS/XLSX files only the first sheet is read and headers are expected to be in the first row with data starting in the second row.
For CSV, currently only files which use comma as delimiter and double-quotes for quoting text are supported. Other formats require code-changes, but should be easy to do by adjusting the CSFFormat definition (this project uses Apache Commons CSV for CSV handling).
The older .doc format is not supported as template document because this project makes heavy use of the internal XML format of DOCX files.
The resulting output file is fully held in memory, so a very large number of merged documents may cause very high memory usage and/or out-of-memory errors.
A streaming writing is currently not easy to support, but it should be possible to add a mode of operation which writes separate files for the merged documents to overcome this limitation if necessary. Pull-requests highly welcome!
If there are multiple formattings applied to a strings that holds a template-pattern, (e.g. if you make only half of the template-variable bold), the resulting XML-representation of the document might be split into multiple XML-Tags and thus might prevent the replacement from happening.
A workaround is to use the formatting tool in LibreOffice/OpenOffice to ensure that the replacement tags have only one formatting applied to them.
See centic9#6 for possible improvements.
./gradlew eclipse
cd poi-mail-merge
./gradlew check jacocoTestReport
- poi-mail-merge is licensed under the BSD 2-Clause License.