How to create a report generator in word

前端 未结 1 1565
孤独总比滥情好
孤独总比滥情好 2021-01-25 00:26

I am currently trying to figure out to to make it so I can load a template for word and be prompted to fill in data that will then be populated on the word document. The report

相关标签:
1条回答
  • 2021-01-25 01:07

    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.

    0 讨论(0)
提交回复
热议问题