<p>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?</p>

[{'answer_id': 10423, 'author': 'rjzii', 'author_id': 1185, 'author_profile': 'https://Stackoverflow.com/users/1185', 'pm_score': 7, 'selected': True, 'text': '<p>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 <a href="http://support.microsoft.com/kb/257757" rel="noreferrer">using Office Automation on a server</a>.</p>\n\n<p>However, if you are working on the client machine then you have a choice of either <a href="http://msdn.microsoft.com/en-us/office/default.aspx" rel="noreferrer">using Office Automation</a> 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. </p>\n\n<p>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.</p>\n\n<p>Office Open XML Iinformation</p>\n\n<ul>\n<li>Office Open XML - <a href="http://en.wikipedia.org/wiki/Office_Open_XML" rel="noreferrer">http://en.wikipedia.org/wiki/Office_Open_XML</a></li>\n<li>OpenXML Developer - <a href="http://openxmldeveloper.org/default.aspx" rel="noreferrer">http://openxmldeveloper.org/default.aspx</a></li>\n<li>Introducing the Office (2007) Open XML File Formats - <a href="http://msdn.microsoft.com/en-us/library/aa338205.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/aa338205.aspx</a></li>\n</ul>\n'}, {'answer_id': 10424, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 1, 'selected': False, 'text': '<p>@Dale Ragan: That will work for the Office 2003 XML format, but that\'s not portable (as, say, .doc or .docx files would be).</p>\n\n<p>To read/write those, you\'ll need to use the Word Object Library ActiveX control:</p>\n\n<p><a href="http://www.codeproject.com/KB/aspnet/wordapplication.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/aspnet/wordapplication.aspx</a></p>\n'}, {'answer_id': 10438, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 1, 'selected': False, 'text': '<p>@Danny Smurf:  Actually this article describes what will become the Office Open XML format which Rob answered with.  I will pay more attention to the links I post for now on to make sure there not obsolete.  I actually did a search on WordML, which is what it was called at the time.</p>\n\n<p>I believe that the Office Open XML format is the best way to go.</p>\n'}, {'answer_id': 10472, 'author': 'Luke Girvin', 'author_id': 133, 'author_profile': 'https://Stackoverflow.com/users/133', 'pm_score': 2, 'selected': False, 'text': '<p>Schmidty, if you want to generate Word documents on a web server you will need a licence for each <strong>client</strong> (not just the web server).  See this section in the first <a href="http://support.microsoft.com/kb/257757" rel="nofollow noreferrer">link</a> Rob posted:</p>\n\n<p>"Besides the technical problems, you must also consider licensing issues. Current licensing guidelines prevent Office applications from being used on a server to service client requests, unless those clients themselves have licensed copies of Office. Using server-side Automation to provide Office functionality to unlicensed workstations is not covered by the End User License Agreement (EULA)."</p>\n\n<p>If you meet the licensing requirements, I think you will need to use COM Interop - to be specific, the <a href="http://msdn.microsoft.com/en-us/library/aa163987.aspx" rel="nofollow noreferrer">Office XP Primary Interop Assemblies</a>.</p>\n'}, {'answer_id': 11136, 'author': 'DavidWhitney', 'author_id': 1297, 'author_profile': 'https://Stackoverflow.com/users/1297', 'pm_score': 3, 'selected': False, 'text': "<p>Have you considered using .RTF as an alternative?</p>\n\n<p>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. </p>\n\n<p>Your end users probably won't notice.</p>\n"}, {'answer_id': 11155, 'author': 'John', 'author_id': 33, 'author_profile': 'https://Stackoverflow.com/users/33', 'pm_score': 3, 'selected': False, 'text': '<p>I have found <a href="http://www.aspose.com/categories/file-format-components/aspose.words-for-.net-and-java/default.aspx" rel="nofollow noreferrer">Aspose Words</a> 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.</p>\n\n<p>It is a pay-for component but has great support. The other alternative as already suggested is RTF.</p>\n'}, {'answer_id': 11162, 'author': 'DancesWithBamboo', 'author_id': 1334, 'author_profile': 'https://Stackoverflow.com/users/1334', 'pm_score': 2, 'selected': False, 'text': '<p>Check out VSTO (Visual Studio Tools for Office).  It is fairly simple to create a Word template, inject an xml data island into it, then send it to the client.  When the user opens the doc in Word, Word reads the xml and transforms it into WordML and renders it.  You will want to look at the ServerDocument class of the VSTO library.  No extra licensing is required from my experience.</p>\n'}, {'answer_id': 62167, 'author': 'littlecharva', 'author_id': 366, 'author_profile': 'https://Stackoverflow.com/users/366', 'pm_score': 2, 'selected': False, 'text': "<p>I currently do this exact thing.  </p>\n\n<p>If the document isn't very big, doesn't contain images and such, then I store it as an RTF with #MergeFields# in it and simply replace them with content, sending the result down to the user as an RTF.</p>\n\n<p>For larger documents, including images and dynamically inserted images, I save the initial  Word document as a Single Webpage *.mht file containing the #MergeFields# again.  I then do the same as above.  Using this, I can easily render a DataTable with some basic Html table tags and replace one of the #MergeFields# with a whole table.  </p>\n\n<p>Images can be stored on your server and the url embedded into the document too.</p>\n\n<p>Interestingly, the new Office 2007 file formats are actually zip files - if you rename the extension to .zip you can open them up and see their contents.  This means you should be able to switch content such as images in and out using a simple C# zip library.</p>\n"}, {'answer_id': 393213, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>I faced this problem and created a small library for this. It was used in several projects and then I decided to publish it. It is free and very very simple but I\'m sure it will help with you with the task. Invoke the Office Open XML Library, <a href="http://invoke.co.nz/products/docx.aspx" rel="nofollow noreferrer">http://invoke.co.nz/products/docx.aspx</a>.</p>\n'}, {'answer_id': 732749, 'author': 'Jafin', 'author_id': 40513, 'author_profile': 'https://Stackoverflow.com/users/40513', 'pm_score': 2, 'selected': False, 'text': '<p>I have had good success using the Syncfusion Backoffice DocIO which supports doc and docx formats.  </p>\n\n<p>In prior releases it did not support everything in word, but accoriding to your list we tested it with tables and text as a mail merge approach and it worked fine.</p>\n\n<p>Not sure about the import of images though.  On their blurb page <a href="http://www.syncfusion.com/products/DocIO/Backoffice/features/default.aspx" rel="nofollow noreferrer">http://www.syncfusion.com/products/DocIO/Backoffice/features/default.aspx</a> it says </p>\n\n<blockquote>\n  <p>Blockquote\n  Essential DocIO has support for inserting both Scalar and Vector images into the document, in almost all formats. Bitmap, gif, png and tiff are some of the common image types supported. </p>\n</blockquote>\n\n<p>So its worth considering.</p>\n\n<p>As others have mentioned you can build up a RTF document, there are some good RTF libraries around for .net like <a href="http://www.codeproject.com/KB/string/nrtftree.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/string/nrtftree.aspx</a></p>\n'}, {'answer_id': 887971, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>I have spent the last week or so getting up to speed on <a href="http://en.wikipedia.org/wiki/Office_Open_XML" rel="noreferrer">Office Open XML</a>. We have a database application that stores survey data that we want to report in Microsoft Word. You can actually create Word 2007 (docx) files from scratch in C#. The Open XML SDK version 2 includes a cool application called the Document Reflector that will actually provide the C# code to fully recreate a Word document. You can use parts or all of the code, and substitute the bits you want to change on the fly. The help file included with the SDK has some good code samples as well.</p>\n\n<p>There is no need for the Office Interop or any other Office software on the server - the new formats are 100% XML.</p>\n'}, {'answer_id': 1829494, 'author': 'Marcello Belguardi', 'author_id': 192483, 'author_profile': 'https://Stackoverflow.com/users/192483', 'pm_score': 3, 'selected': False, 'text': '<p>To generate Word documents with Office Automation within .NET, specifically in C# or VB.NET:</p>\n\n<ol>\n<li><p>Add the <strong>Microsoft.Office.Interop.Word</strong> assembly reference to your project. The path is <strong>\\Visual Studio Tools for Office\\PIA\\Office11\\Microsoft.Office.Interop.Word.dll</strong>.</p></li>\n<li><p>Follow the Microsoft code example \nyou can find here: <a href="http://support.microsoft.com/kb/316384/en-us" rel="noreferrer">http://support.microsoft.com/kb/316384/en-us</a>.</p></li>\n</ol>\n'}, {'answer_id': 3310380, 'author': 'MadBoy', 'author_id': 218540, 'author_profile': 'https://Stackoverflow.com/users/218540', 'pm_score': 4, 'selected': False, 'text': '<p><a href="http://docx.codeplex.com/" rel="nofollow noreferrer">DocX free library</a> for creating DocX documents, actively developed and very easy and intuitive to use. Since CodePlex is dying, project has moved to <a href="https://github.com/WordDocX/DocX" rel="nofollow noreferrer">github</a>.</p>\n'}, {'answer_id': 5973109, 'author': 'Eric White', 'author_id': 660010, 'author_profile': 'https://Stackoverflow.com/users/660010', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve written a <a href="http://ericwhite.com/blog/map/generating-open-xml-wordprocessingml-documents-blog-post-series/" rel="nofollow">blog post series on Open XML WordprocessingML document generation</a>.  My approach is that you create a template document that contains content controls, and in each content control you write an XPath expression that defines how to retrieve the content from an XML document that contains the data that drives the document generation process.  The code is free, and is licensed under the the <a href="http://en.wikipedia.org/wiki/Shared_source" rel="nofollow">Microsoft Reciprocal License (Ms-RL)</a>.  In that same blog post series, I also explore an approach where you write C# code in content controls.  The document generation process then processes the template document and generates a C# program that generates the desired documents.  One advantage of this approach is that you can use <em>any</em> data source as the source of data for the document generation process.  That code is also licenced under the Microsoft Reciprocal License.</p>\n'}, {'answer_id': 8119852, 'author': 'plaes', 'author_id': 157176, 'author_profile': 'https://Stackoverflow.com/users/157176', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://www.libreoffice.org/" rel="nofollow">LibreOffice</a> also supports headless interaction via API. Unfortunately there\'s currently not much information about this feature yet.. :(</p>\n'}, {'answer_id': 10923315, 'author': 'David Thielen', 'author_id': 509627, 'author_profile': 'https://Stackoverflow.com/users/509627', 'pm_score': 0, 'selected': False, 'text': '<p>Another alternative is <a href="https://www.windwardstudios.com/programming-languages/csharp" rel="nofollow noreferrer">Windward Docgen</a> (disclaimer - I\'m the founder). With Windward you design the template in Word, including images, tables, graphs, gauges, and anything else you want. You can set tags where data from an XML or SQL datasource is inserted (including functionality like forEach loops, import, etc). And then generate the report to DOCX, PDF, HTML, etc.</p>\n'}, {'answer_id': 14059899, 'author': 'Ricardo', 'author_id': 1703691, 'author_profile': 'https://Stackoverflow.com/users/1703691', 'pm_score': 1, 'selected': False, 'text': '<p>You could also use Word document generator. It can be used for client-side or server-side deployment. From the project description:</p>\n\n<blockquote>\n  <p>WordDocumentGenerator is an utility to generate Word documents from\n  templates using Visual Studio 2010 and Open XML 2.0 SDK.\n  WordDocumentGenerator helps generate Word documents both\n  non-refresh-able as well as refresh-able based on predefined templates\n  using minimum code changes. Content controls are used as placeholders\n  for document generation. It supports Word 2007 and Word 2010.</p>\n</blockquote>\n\n<p>Grab it: <a href="http://worddocgenerator.codeplex.com/" rel="nofollow">http://worddocgenerator.codeplex.com/</a></p>\n\n<p>Download SDK: <a href="http://www.microsoft.com/en-us/download/details.aspx?id=5124" rel="nofollow">http://www.microsoft.com/en-us/download/details.aspx?id=5124</a></p>\n'}]
['https://Stackoverflow.com/questions/10412', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/354/']