Repeating content with Office Open XML

我的梦境 提交于 2019-12-10 10:45:45

问题


I've been checking out what is possible with the Office Open XML specification for documents. I'm particularly interested in being able to add custom XML content to a document and binding it to content controls. I was wondering if it is possible to have a repeating content control type?

For example, say I have some custom XML in my .docx file that looks like so:

<Work>
  .
  .
  <People>
    <Person>
      <Name>Jane Doe</Name>
      <EmailAddress>jane@abc.com</EmailAddress>
    </Person>
    <Person>
      <Name>John Doe</Name>
      <EmailAddress>john@xyz.com</EmailAddress>
    </Person>
  </People>
</Work>

Is there any repeating content control (similar to an ASP repeater) that I could use to generate output that looks something like

  • Jane Doe (jane@abc.com)
  • John Doe (john@xyz.com)

If there are no types of controls, I guess I could do some processing on the word document itself to generate a list. Has anyone done this and have some helpful pointers?


回答1:


As 0xA3 says, there is nothing built-in like this, it would be WordprocessingML-manipulated. But here is an example of pulling data from a database to create multiple entries: Pushing Data from a Database into a Word Document.

Also, on the subject of databinding in general, here is a good starter tutorial: Linking Word 2007 Content Controls to Custom XML.




回答2:


With Word 2013, there are some improvement for content controls. Including a repeating content control. I haven't test it.

http://msdn.microsoft.com/en-us/library/office/jj889465.aspx#WordCC_RepeatingSection

With Word 2007 and 2010, you have to do some extraprocessing for creating rows with content control and bind them with generated Xpath query. You can check the following video.

http://www.microsoft.com/uk/msdn/screencasts/screencast/261/Office-Open-XML-Content-Controls-and-Repeating-Data-Part-1-of-3.aspx




回答3:


http://dev.plutext.org/svn/docx4j/trunk/docx4j/sample-docs/databinding/conventions.html proposes a convention for doing this. You do need to pre-process the docx to clone the content control n times; currently there is code for doing this via a Word macro, or in Java (docx4j).




回答4:


There is no built-in Content Control for repeating content in Word 2007/2010. The following possibilities come to my mind though:

  • Create the full content programmatically using the Open XML SDK. No databinding though.
  • Use Word's MailMerge feature for displaying several similar data sets.
  • Use InfoPath which has better support for repeating content
  • Use a third-party solution such as Windward Reports.

Update: Word 2013 now has a content control for repeating sections.



来源:https://stackoverflow.com/questions/3859655/repeating-content-with-office-open-xml

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