How to place labels in the ms word template and fill them with data from the database

I need to create an application in which users download a specific document template (Word, etc.), and place in it controls (labels, text fields) with specific identifiers and based on the identifiers of the controls, I have to fill out a template with data from SQL server, and then make the document filled with data available for download.

Anyway, I can use C # and APS.NET, Javascript, JQuery, etc.

I don’t know where to start.

Thanks in advance.

+4
source share
3 answers

I searched a bit and found out that the solution to my problem is Open XML SDK 2.0 for Microsoft Office

I have never used it, and I think I need to search a lot until I can use it in my application. Hope it will work fine.

+1
source

You can make a document from a template as follows:

priavte void CreateWordDocument(string InputFileNamePath, string OutputFileNamePath) { Application app = new Microsoft.Office.Interop.Word.Application(); doc = app.Documents.Open(InputFileNamePath,ref missing, ref missing,ref Missing, ref Missing); // Activate document doc.Actiavte(); //Find place holders in input template and replace them with database values this.FindAndReplace(app,"<Name>","John"); //take all values from database this.FindAndReplace(app,"<Address>","Test address"); this.FindAndReplace(app,"<City>","Test City"); //Save file doc.SaveAs(ref OutputFileNamePath, ref missing, ref missing, ref missing, ref missing); doc.Close(ref missing,ref missing,ref missing,) } 

Visit the link for more help: http://www.techrepublic.com/blog/howdoi/how-do-i-modify-word-documents-using-c/190

+3
source

When you code it yourself in C #, please also pay attention to the following aspects:

  • Headers and footers deserve separate processing when working with a document directly. When you have several identical values ​​on the page of a repeating element, it was easier for me to always put the first specific value in the header and the last defined value in the footer when they have a link.
  • Do not try to use XML representation; Headers and footers are much more difficult to process in an XML document when replacing the original XML.
  • Tables can cause problems; when you use the text representation of a document to analyze the content (and which is required when you want to insert duplicate rows), the tables introduce an offset between the text representation and the cursor position in the document. You will need to add additional bindings inside the table manually or use the XML view.
  • Or use TAB tables instead of tables in the document if you need a table similar in structure.
  • When you need to repeat pictures or frames or other more complex functions, they are simpler than tables. If you have only a limited list of used images, such as CxO-s signature, put them all in a Word document and delete those that are not needed at all or do not need to be repeated.

For downloading, I recommend that you remember to set the metadata properties (which simplifies life for document management systems) and use a web service or similarly for processing it on the backend.

0
source

Source: https://habr.com/ru/post/1439989/


All Articles