- First download and install Open XML SDK 2.0 for Microsoft Office
- Open the installed program, mine was located at C:\Program Files (x86)\Open XML SDK\V2.0\tool\OpenXmlSdkTool.exe
- With the tool, open the desired word document that you want to generate with visual studio, temp.docx for example.
- Click on 'Reflect Code' on the top pane. Notice how there will be code generated on the right hand frame.
- In Visual Studio, create a new console application project, name it for example HelloWord. Add a new empty class WordDoc.cs and copy the above generated code to this new class file. Rename namespace to HelloWord and class to WordDoc. Here's how it looks like
-
Add the following two references
- C:\Program Files (x86)\Open XML SDK\V2.0\lib\DocumentFormat.OpenXml.dll
- WindowsBase from .NET (path: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll)
-
add these two lines to the Main function
WordDoc wd = new WordDoc(); wd.CreatePackage(@"C:\temp\mydoc.docx");
Once you run the console, you should see mydoc.docx created in C:\temp.
Wednesday, 23 May 2012
How to create a word document with c# code
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment