开发者

populating word doc from xml in c#

how do i populate a word doc from an xml template using c#. I know how to open a blank document but I cant figure out how to populate it.

Word.Document wordDoc = wordApp.Documents.Add(ref objMissing,
      开发者_如何转开发       ref objMissing, ref objMissing, ref objMissing);
Word.Paragraph wordParagraph = wordDoc.Paragraphs.Add(ref objMissing);

Here's a basic look at my xml form.

<?xml version="1.0" encoding="utf-8"?>
<Cover>
  <CaseNo>Case No:</CaseNo>
  <Title>Title:</Title>
  <Date>Date:</Date>
</Cover>

What I eventually want to do is get the user to fill in textboxes in a c# program which will populate the word document.

The information is not coming from an sql database


You can use the http://msdn.microsoft.com/en-us/library/bb448854.aspx to generate docx files. It is easy.

What you also can do when using Office 2007 or 2010, is:

  • Create the word document in Microsoft Office and rename the word document from docx to zip.
  • Open it, and you will find several xml files. Find the xml which contains the actual document. Add {0},{1} etc. placeholders so that you can use string.Format to insert your data at the correct place in the document. Your application than enters the data in the placeholders and rezip the xml files into a docx file.


You'll need to identify somewhere in your word document for the information to go...

Firstly, you'll need to make a selection in the Word Document, then with that selection you can simply use the TypeText method to get your data across.

What I have done in these circumstances, and I find users feel comfortable with, is to define a set of placeholders (for example {caseno} or {title} ) in a Word template, something they can edit. Then you can search for the placeholder and then replace it with the text from your XML file - Have a look at Word.Selection.Find

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜