views:

1841

answers:

9

I am planning on generating a Word document on the webserver dynamically. Is there good way of doing this in c#? I know I could script Word to do this but I would prefer another option.

+8  A: 

You'd be better off generating an rtf file, which word will know how to open.

Brian
Yup, and you are much less dependent of different word versions.
Gamecat
I upvoted this earlier when I thought you said "You'd be better off generating an rtf file, which the world will know how to open."
Karl
+1  A: 

There are third party libraries about that will do the job.

Doing a quick google came up with this one, for example.

I haven't tried any, so I can't give you specific advice, I'm afraid!

Let us know how you get on...

Chris Roberts
+7  A: 

If want to generate Office 2007 documents check the Open XML File Formats, they're simple zipped XML files, check this links:

Edit: Check this project, can serve you as a good starting point:

Seems very simple and customizable, look this code snippet:

Paragraph p = new Paragraph();
p.Runs.Add(new Run("Text can have multiple format styles, they can be "));
p.Runs.Add(new Run("bold and italic", 
        TextFormats.Format.Bold | TextFormats.Format.Italic));
doc.Paragraphs.Add(p);
CMS
I recommend this approach, it is a bit complicated, but its free and works well. I use XML to create Excel documents as well.
Mitchel Sellers
I cannot imagine why you would recommend this. Unless you know all clients have Word2007 or the importer installed, it is useless. The format is a 5000 page specification.
Stephan Eggermont
+5  A: 

Word will quite happily open a HTML with a .doc extension. If you include an internal style sheet, you can have it fully formatted. There was previous post on this subject:

http://stackoverflow.com/questions/282531/export-to-word-document-in-c#283932

Remou
+1  A: 

In Office 2007 Microsoft introduced a new file format called the Microsoft Open Office XML Format (.docx). This format is not compatible with older versions of Microsoft Word. Since this is XML you can create or read with out having a Word installed.

Jobi Joy
You haven't seen it, I guess? 5K pages for a specification with a few thousand bugs
Stephan Eggermont
+5  A: 

Creating the old .DOC files (pre-Word 2007) is nigh-impossible without Word itself. The format is just too complex. Microsoft has released the format description, but it's enough to reduce a grown programmer to tears. There is a reason for that too (historical), but that doesn't make things better.

The new .DOCX would be easier, although quite a bit of hassle still. However depending on which Word versions you are targeting, there are some other options too.

For one, there is the classic .RTF. The format is pretty complex still, yet well documented and has strong support across many applications and platforms. And you might use some string-replacing into template files to make things easier (it's non-binary).

Then there are the "old" Word XML files. I think they worked starting with Word XP. Kinda the predecessors of .DOCX. I've used them, not bad. And the documentation is pretty OK.

Finally, the easy way that I would choose, is to make a simple HTML. Word can load HTML files just fine starting with version 2000. In the simplest way just change the extension of a HTML file to .DOC and you have it. You can also add a few word-specific tags and comments to make it look even better in Word. Use the Word's Save As...HTML option to see what they are.

Vilx-
docx would not be easier. Translating from binary to xml is a complexity preserving operation.
Stephan Eggermont
+16  A: 

I've worked at a company in the past that really wanted generated word documents, in the end they were perfectly satisfied with RTF docs that had a ".doc" extension. Word has no problem recognizing and opening them.

The RTF docs were generated with iText.net (free .net library), the API is pretty easy to use, performs extremely well, you don't need word on the machine, also, you could extend to generating PDF, HTML, and Text docs in the future with very little effort. After four years the solution I created is still in place, so that's a little testimony in iText.net's favor.

It looks like the official iText page suggests that iText Sharp is the best .Net choice right now, so that's another option

JB Brown
i'm finding that documentation on this project is non-existent.
benpage
A: 

Here is the component that generates document based on the custom template. The documents are generated from the sharepoint list ... so the data is pulled from the list item into the document on the fly: http://store.sharemuch.com/products/generate-word-documents-from-sharepoint-list

Hope that helps,

Yaroslav Pentsarskyy Blog: www.sharemuch.com

+2  A: 

With Aspose.Words you can generate a document using a rich public API or the mail merge-like reporting feature or in a combination.

romeok