[ACCEPTED]-How can a Word document be created in C#?-openxml

Accepted answer
Score: 49

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

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

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

Office 1 Open XML Iinformation

Score: 17

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

Score: 14

I have spent the last week or so getting 15 up to speed on Office Open XML. We have a database application 14 that stores survey data that we want to 13 report in Microsoft Word. You can actually 12 create Word 2007 (docx) files from scratch 11 in C#. The Open XML SDK version 2 includes 10 a cool application called the Document Reflector 9 that will actually provide the C# code to 8 fully recreate a Word document. You can 7 use parts or all of the code, and substitute 6 the bits you want to change on the fly. The 5 help file included with the SDK has some 4 good code samples as well.

There is no need 3 for the Office Interop or any other Office 2 software on the server - the new formats 1 are 100% XML.

Score: 10

Have you considered using .RTF as an alternative?

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

Your 1 end users probably won't notice.

Score: 10

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

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

Score: 8

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

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

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

Score: 4

Schmidty, if you want to generate Word documents 15 on a web server you will need a licence 14 for each client (not just the web server). See 13 this section in the first link Rob posted:

"Besides 12 the technical problems, you must also consider 11 licensing issues. Current licensing guidelines 10 prevent Office applications from being used 9 on a server to service client requests, unless 8 those clients themselves have licensed copies 7 of Office. Using server-side Automation 6 to provide Office functionality to unlicensed 5 workstations is not covered by the End User 4 License Agreement (EULA)."

If you meet 3 the licensing requirements, I think you 2 will need to use COM Interop - to be specific, the 1 Office XP Primary Interop Assemblies.

Score: 3

Check out VSTO (Visual Studio Tools for 8 Office). It is fairly simple to create 7 a Word template, inject an xml data island 6 into it, then send it to the client. When 5 the user opens the doc in Word, Word reads 4 the xml and transforms it into WordML and 3 renders it. You will want to look at the 2 ServerDocument class of the VSTO library. No 1 extra licensing is required from my experience.

Score: 3

I faced this problem and created a small 5 library for this. It was used in several 4 projects and then I decided to publish it. It 3 is free and very very simple but I'm sure 2 it will help with you with the task. Invoke 1 the Office Open XML Library, http://invoke.co.nz/products/docx.aspx.

Score: 3

I have had good success using the Syncfusion 15 Backoffice DocIO which supports doc and 14 docx formats.

In prior releases it did 13 not support everything in word, but accoriding 12 to your list we tested it with tables and 11 text as a mail merge approach and it worked 10 fine.

Not sure about the import of images 9 though. On their blurb page http://www.syncfusion.com/products/DocIO/Backoffice/features/default.aspx it says

Blockquote Essential 8 DocIO has support for inserting both Scalar 7 and Vector images into the document, in 6 almost all formats. Bitmap, gif, png and 5 tiff are some of the common image types 4 supported.

So its worth considering.

As others 3 have mentioned you can build up a RTF document, there 2 are some good RTF libraries around for .net 1 like http://www.codeproject.com/KB/string/nrtftree.aspx

Score: 3

I've written a blog post series on Open XML WordprocessingML document generation. My approach is that you 17 create a template document that contains 16 content controls, and in each content control 15 you write an XPath expression that defines 14 how to retrieve the content from an XML 13 document that contains the data that drives 12 the document generation process. The code 11 is free, and is licensed under the the Microsoft Reciprocal License (Ms-RL). In 10 that same blog post series, I also explore 9 an approach where you write C# code in content 8 controls. The document generation process 7 then processes the template document and 6 generates a C# program that generates the 5 desired documents. One advantage of this 4 approach is that you can use any data source 3 as the source of data for the document generation 2 process. That code is also licenced under 1 the Microsoft Reciprocal License.

Score: 2

I currently do this exact thing.

If the 20 document isn't very big, doesn't contain 19 images and such, then I store it as an RTF 18 with #MergeFields# in it and simply replace 17 them with content, sending the result down 16 to the user as an RTF.

For larger documents, including 15 images and dynamically inserted images, I 14 save the initial Word document as a Single 13 Webpage *.mht file containing the #MergeFields# again. I 12 then do the same as above. Using this, I 11 can easily render a DataTable with some 10 basic Html table tags and replace one of 9 the #MergeFields# with a whole table.

Images 8 can be stored on your server and the url 7 embedded into the document too.

Interestingly, the 6 new Office 2007 file formats are actually 5 zip files - if you rename the extension 4 to .zip you can open them up and see their 3 contents. This means you should be able 2 to switch content such as images in and 1 out using a simple C# zip library.

Score: 1

@Dale Ragan: That will work for the Office 4 2003 XML format, but that's not portable 3 (as, say, .doc or .docx files would be).

To 2 read/write those, you'll need to use the 1 Word Object Library ActiveX control:


Score: 1

@Danny Smurf: Actually this article describes 8 what will become the Office Open XML format 7 which Rob answered with. I will pay more 6 attention to the links I post for now on 5 to make sure there not obsolete. I actually 4 did a search on WordML, which is what it 3 was called at the time.

I believe that the 2 Office Open XML format is the best way to 1 go.

Score: 1

LibreOffice also supports headless interaction via 2 API. Unfortunately there's currently not 1 much information about this feature yet.. :(

Score: 1

You could also use Word document generator. It 11 can be used for client-side or server-side 10 deployment. From the project description:

WordDocumentGenerator 9 is an utility to generate Word documents 8 from templates using Visual Studio 2010 7 and Open XML 2.0 SDK. WordDocumentGenerator 6 helps generate Word documents both non-refresh-able 5 as well as refresh-able based on predefined 4 templates using minimum code changes. Content 3 controls are used as placeholders for 2 document generation. It supports Word 2007 1 and Word 2010.

Grab it: http://worddocgenerator.codeplex.com/

Download SDK: http://www.microsoft.com/en-us/download/details.aspx?id=5124

Score: 0

Another alternative is Windward Docgen (disclaimer - I'm 7 the founder). With Windward you design the 6 template in Word, including images, tables, graphs, gauges, and 5 anything else you want. You can set tags 4 where data from an XML or SQL datasource 3 is inserted (including functionality like 2 forEach loops, import, etc). And then generate 1 the report to DOCX, PDF, HTML, etc.

More Related questions