Search code examples
docxmailmerge

How to feed Word 2010 (.docx) documents/templates with data from MySQL database?


What would be the best approach to replace placeholders in a .docx document (Word 2010) with data coming from a MySQL database?

Can I just open the file using a server side language and do a string replace per each placeholder?

Is there any existing tool/library available?

Thanks


Solution

  • If you want to use PHP the most complete available solution is PHPDocX.

    You may check in the tutorial how to substitute placeholder variables by data coming from any data source (like a MySQL DB).

    In particular, you may populate table rows with an indefinite number of entries and you may delete whole blocks of the Word document depending on the data fed to the application or build dynamical Word charts.

    You may check the available DEMO for a simple but quite illustrative example (its inner workings are explained in the tutorial section).