开发者

Options for automating Microsoft Word 2007 document creation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

Closed 8 years ago.

开发者_JAVA技巧 Improve this question

I have automated word document creation from templates using the "Microsoft Word 12.0 Object Library" in my project, but it is unacceptably slow. I'm looking for alternatives: I need to create a document from a template, run some macros and insert text and tables into various bookmark ranges (some text formatting options would be nice too). I only need the final document to be compatible with Word2007, but would prefer a solution easily available from .NET (other recommendations are appreciated too)...

Please advise.


Have a look at Microsoft's Office OpenXML SDK. It allows you to create Word 2007 (.docx) documents programmatically without the Office applications.

To use it you will need to be familiar with the OpenXML file format. A good starting point also providing examples are the blogs of Brian Jones and Erika Ehrli.

This sample on CodeProject might also be a good start: Creation of a Word 2007 document using the Open XML Format SDK.

I also recommend you to have a look at the DocumentReflector, a tool included within the SDK, that creates the code to produce a certain Word document based on an existing document.


Options, I used up to now:

  1. Yours - COM automation: performance wasn't the biggest issue here for me, just all the ugly details of COM automation like dead processes, locked files, ...; for some tasks where an API exists and as long as only person uses the app, its still an option. Requires Word installation.
  2. Import as HTML and save as Word - works quick and better as expected, as long as your word format is simple and matches HTML markup structure quite well (e.g. headers, item lists). To simplify handling, templates have to be HTML then, so often a quick hack for a limited use case. Requires Word installation.
  3. Concatenate and process templates saved as WordML: saves you the pain of COM automation, but since WordML is just a binary format masked as XML, it's often a pain; hence some tasks are more complicated than using a Word-API. Nevertheless I prefer it over 1. Does not require a Word installation.
  4. As in previous answer, OpenXML SDK: just starting to use this, seems to be a good compromise of 1 and 3: work on XML level or on API level - whatever fits best for a certain feature. Does not require a Word installation.


other recommendations are appreciated too

The Java equivalents to the OpenXML SDK are docx4j (my project), and POI.

Using docx4j, you can inject macros into an existing docx, or work from a dotm (as divo suggests).


Officewriter is another option.

The OfficeWriter API is a .NET library that allows you to generate, manipulate, and read Excel and Word documents from your own applications.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜