DiscountASP
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.

Creating Word file using Office Open XML

by Farooq Kaiser 28. May 2009 14:40

Recently, i created a webservice to generate the word and excel files using Office Open XML.

The Open XML SDK can be downloaded from http://www.microsoft.com/downloads/details.aspx?familyid=ad0b72fb-4a1d-4c52-bdb5-7dd7e816d046&displaylang=en  

Use the following namespaces 
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;

Code sample :

using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(@"d:\doc\hello.docx", WordprocessingDocumentType.Document))
{
MainDocumentPart part = wordDoc.AddMainDocumentPart();
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
sb.Append("<w:document xmlns:w=\"
http://schemas.openxmlformats.org/wordprocessingml/2006/main\">");
sb.Append("<w:body><w:p><w:r><w:t>Hello world!</w:t></w:r></w:p></w:body>");
sb.Append("</w:document>");
using (Stream stream = part.GetStream())
{
byte[] buf = (new UTF8Encoding()).GetBytes(sb.ToString());
stream.Write(buf, 0, buf.Length);
}
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | C# | XML

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Jobs Autos Real estate Videos Power by Google