How Can a Word Document Be Created in C#

How can a Word document be created in C#?

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

  • Office Open XML - http://en.wikipedia.org/wiki/Office_Open_XML
  • OpenXML Developer - http://openxmldeveloper.org/default.aspx
  • Introducing the Office (2007) Open XML File Formats - http://msdn.microsoft.com/en-us/library/aa338205.aspx

Create, insert text and save a Word doc in C#

The Microsoft MSDN documentation has tons of useful guides and examples.

You are going to want to include:

using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Word;

Then declare your application:

Word.Application app = new Word.Application();

Declare your new document:

Word.Document doc = app.Documents.Add();

Add text to your documnet

There are two ways to save these documents:

Programmatically

Using a save file dialog box

Generate Word document from c#

There are third-party libraries to help, but if you want to develop a solution yourself, you may want to get hold of the Open XML SDK.

I recommend that rather than creating a Word document completely in code, you create a template document using Microsoft Word and then modify it using the Open XML SDK.

Don't be tempted to use Word Automation if you're producing the document on a server. Apart from being unsupported, it's somewhat prone to resource issues.

I blogged some time ago about inserting images into Word documents using Open XML.

Creation of Word Document file

just refer the following links

http://nishantrana.wordpress.com/2007/11/03/creating-word-document-using-c/

http://2leggedspider.wordpress.com/2004/06/30/creating-a-word-document-using-aspnet-and-c/

Create word documents in C#

Have you tried mail merge? This way you can have a word template set and you can position your images and table just like you want. Then you only have to worry about merging your input fields with the template. And this way you can have multiple templates for different word documents if required. Here's an example:

Sending text to Mail-Merge Fields in Microsoft Word 2010

Creating a Word document from code

Creating and Opening Word Documents with C#

.NET 4.0 makes it easier also

from Open MS Word Document in MS Word from a C# Windows Form

C# word document clone existing word file as a new document

This can be achieved by passing a file path to the Docments.Add method:

Word.Application _word = new Word.Application();
_word.Visible = true;
_word.WindowState = Word.WdWindowState.wdWindowStateMaximize;

Word.Document _doc = _word.Documents.Add(pathToExistingDocument);

How to create a word document using html written in C#

As requested by the OP, and to make easier for others to find this solution, here it goes the answer I posted as a comment (plus extra results from testing):

When opening an HTML file, MS Word honors the CSS properties page-break-before and page-break-after. There is a caveat, however:

On "Web design" view, page-breaks are never shown (this doesn't mean that they aren't there), just like browsers don't "show" them. And Word opens html files on Web design view by default (which quite makes sense). You need to print the document or switch to some other view (typicall "Print design") to see your breaks in all their glory.

So, saving an HTML file with a .doc extension is a viable solution (also tested: Word opens it properly despite of the extension).

Note: all the testing was done on MS Word 2003 using this snippet: <html>asdf<br style="page-break-before: always;">new page!</html>



Related Topics



Leave a reply



Submit