Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill a word document in asp.net?

Tags:

asp.net

I am working on Asp.Net project which needs to fill in a word document. My client provides a word template with last name, firstname, birth date,etc... . I have all those information in the sql database, and the client want the users of the application be able to download the word document with filled in information from the database.

What's the best way to archive this? Basically, I need identify those "fillable spot" in word document, fill those information in when the application user clicks on the download button.

like image 776
J.W. Avatar asked Apr 13 '09 14:04

J.W.


People also ask

What is the method of inserting text in a existing document?

Place the cursor where you want to insert the text. Select Insert > Object > Text from File. In the Insert File dialog box, select a document file. Select Range.


3 Answers

If you can use Office 2007 the way to go is to use the Open XML API to format the documents: http://support.microsoft.com/kb/257757. The reason you have to go that route is that you can't really use Word Automation in a server environment. (you CAN, but it's a huge pain to get working properly, and can EASILY break).

If you can't go the 2007 route, I've actually had pretty good success with just opening up a word template as a stream and finding and replacing the tokens and serving that to the user. This has actually worked surprisingly well in my experience and it's REALLY simple to implement.

like image 188
aquinas Avatar answered Oct 23 '22 00:10

aquinas


I'm not sure about some of the ASP.Net aspects, but I am working on something similar and you might want to look into using an RTF instead. You can use pattern replacement in the RTF. For example you can add a tag like {USER_FIRST_NAME} in the RTF document. When the user clicks the download button, your application can take the information from the database and replace every instance of {USER_FIRST_NAME} with the data from the database. I am currently doing this with PHP and it works great. Word will open the RTF without a problem so that is another reason I chose this method.

like image 42
Mark Avatar answered Oct 23 '22 01:10

Mark


I have used Aspose.Words for .NET. It's a little on the pricey side, but it works extremely well and the API is fairly intuitive for something that is potentially very complex.

If you want to pre-design your documents (or allow others to do that for you), anyone can put fields into the document. Aspose can open the document, find and fill the fields, and save a new filled-out copy for download.

like image 2
Rex M Avatar answered Oct 22 '22 23:10

Rex M