How can a Word document be created in C#?

80,968

Solution 1

The answer is going to depend slightly upon if the application is running on a server or if it is running on the client machine. If you are running on a server then you are going to want to use one of the XML based office generation formats as there are know issues when using Office Automation on a server.

However, if you are working on the client machine then you have a choice of either using Office Automation or using the Office Open XML format (see links below), which is supported by Microsoft Office 2000 and up either natively or through service packs. One draw back to this though is that you might not be able to embed some kinds of graphs or images that you wish to show.

The best way to go about things will all depend sightly upon how much time you have to invest in development. If you go the route of Office Automation there are quite a few good tutorials out there that can be found via Google and is fairly simple to learn. However, the Open Office XML format is fairly new so you might find the learning curve to be a bit higher.

Office Open XML Iinformation

Solution 2

DocX free library for creating DocX documents, actively developed and very easy and intuitive to use. Since CodePlex is dying, project has moved to github.

Solution 3

I have found Aspose Words to be the best as not everybody can open Office Open XML/*.docx format files and the Word interop and Word automation can be buggy. Aspose Words supports most document file types from Word 97 upwards.

It is a pay-for component but has great support. The other alternative as already suggested is RTF.

Solution 4

Have you considered using .RTF as an alternative?

It supports embedding images and tables as well as text, opens by default using Microsoft Word and whilst it's featureset is more limited (count out any advanced formatting) for something that looks and feels and opens like a Word document it's not far off.

Your end users probably won't notice.

Solution 5

To generate Word documents with Office Automation within .NET, specifically in C# or VB.NET:

  1. Add the Microsoft.Office.Interop.Word assembly reference to your project. The path is \Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Word.dll.

  2. Follow the Microsoft code example you can find here: http://support.microsoft.com/kb/316384/en-us.

Share:
80,968
Schmidty
Author by

Schmidty

Updated on July 05, 2022

Comments

  • Schmidty
    Schmidty almost 2 years

    I have a project where I would like to generate a report export in MS Word format. The report will include images/graphs, tables, and text. What is the best way to do this? Third party tools? What are your experiences?

  • Chris B. Behrens
    Chris B. Behrens over 12 years
    Update as of 9/2011...I do both Interop and OpenXml, and faced with this task in either case, I would absolutely use OpenXml. It's the only solution which absolutely guarantees you'll have absolute control over the output.
  • Collin K
    Collin K over 12 years
    I've found Aspose Words to be a bit lacking when working with OpenXml documents. Specifically, its handling of content controls is next to useless, and it cannot handle AltChunk nodes at all.
  • David Thielen
    David Thielen almost 12 years
    Please don't use RTF - the spec is a mess, even Microsoft does not follow it fully, and it has lots of ambiguity in it.
  • Steve Hiner
    Steve Hiner over 7 years
    FYI, when I go to a site for a commercial product and it seems that they hide the price while touting that there is a free trail I automatically assume the product is really expensive and the free trial is just bait. If Docgen is crazy expensive then you're probably right to hide the price but otherwise I think it's a mistake.
  • David Thielen
    David Thielen over 7 years
    @SteveHiner - We have price ranges there (search on price) but we took down the price list because a significant percentage of the time people misunderstood what they needed and then felt mislead. So we figure better to understand someone's need and then give them the right price.
  • David Thielen
    David Thielen about 5 years
    @SteveHiner - You were right. After a lot of people complained we put the prices up on the website, and are trying to explain it well enough they understand what it will be for them.