How to create a report generator in word

China☆狼群 提交于 2019-12-02 03:06:33

There are several approaches to report generation in Word, each with its pros and cons:

1. Automate report generation in MS Word itself (or other VBA enabled MS Office applications), using VBA modules: In this case you will need to have MS Word installed on the machine, where the report generation will take place. You will also have to allow execution of scripts in Word VBA modules. After the document is open, VBA can start executing and there you can do pretty much anything - like connecting to some source of external data, filling this data into the Word template. I have been using bookmarks in Word as placeholders for the data. In this way you can also fill tables - just put the bookmark in the first cell, jump to this bookmark and then programmatically fill data one cell at the time and then programmatically move to the adjacent cell and when you are in the last cell in a row the new row opens and you are in the first cell of the new row. However, you have to be careful how to release WINWORD processes after you are done with document generation. VBA is no longer supported in Office 2013, so this is probably not the best solution.

2. Use Interop assemblies in .NET to do this programmatically: To start with, there is an article on MSDN about using a Word template and manipulate it programmatically. You can find this article here. Again you will need Word installed on the machine where you run the code. You can achieve results very quickly, but note that it is not designed run on a server, because you may soon have performance issues and memory leaks.

3. Use OperXML SDK to programmatically manipulate Word documents from managed .NET application: This gives you total freedom in document generation, but the learning curve is long and steep. Depending on the scale of your business domain you may consider investing your time in learning this technology.

4. Use some of the third party Tools:
In my case I didn't want to use OpenXML. I have replaced VBA-based report automation as explained above with SDK toolkit for Word template generation and report automation. You can check this link for an example. So far this toolkit has covered all our needs, so I am not looking any further. I prepared my first templates about 3 years ago using this toolkit and they smoothly survived migrations from Office 2007 to 2010 to 2013. Now I have a library of about 90 different Word templates which are being used by different applications, using this toolkit in the background. Its kind like 'set it and forget it', and it saves me quite a bit of time compared to VBA document automation which we used before.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!