If somebody like the post and its helpful in your work then, add comments.

Tuesday, July 14, 2009

Open XML : Working with WordProcessingML

To create word document from the open xml (packaging class) is quiet tedious job. MS provided a DocumentFormat.OpenXml assembly for Open XML work. This assembly provides object for Microsoft Word, Microsoft Excel, and Microsoft PowerPoint. It will make easy to developer to create word, excel or PowerPoint reports programmatically.

Add DocumentFormat.OpenXml assembly and start working on creation of the reports. This assembly is provided by the open xml SDK 2.0. It can be downloaded from the Microsoft download center.

The following references are used for creation of the word document from this assembly.

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

We can first start with simple adding text in the word document from the code. To add text in the word document add the assembly reference and then add above namespaces:

First create a paragraph object and set its properties like below

using (WordprocessingDocument WorDocument = WordprocessingDocument.Create(@"c:\Test.docx", WordprocessingDocumentType.Document))
// Add a new main document part.
MainDocumentPart mainPart = WorDocument.AddMainDocumentPart();
//Create Document tree for simple document.
mainPart.Document = new Document();
//Create Body (this element contains other elements that we want to include
Body body = new Body();

FontSize fontsize = new FontSize();
fontsize.Val = 38;
Color TextColor = new Color();
TextColor.Val = "4F81BD";
Bold boldFont = new Bold();
boldFont.Val = DocumentFormat.OpenXml.Wordprocessing.BooleanValues.On;

Paragraph AuthorPara = new Paragraph();
Run run = new Run();
Text TextLine = new Text("Created by: " + System.Environment.UserName);
RunProperties runProps = new RunProperties();
RunFonts runFonts = new RunFonts();
runFonts.Ascii = "Segoe UI";

// Save changes to the main document part.

For creating table in the word document create table object and set properties as below

Table object and properties
Table table = new Table();
TableProperties tblPr = new TableProperties();
TableBorders tblBorders = new TableBorders();
TableWidth tblWidth = new TableWidth();

tblWidth.Width = 550;
tblBorders.TopBorder = new TopBorder();
tblBorders.TopBorder.Val = new EnumValue(BorderValues.BasicBlackDots);
tblBorders.BottomBorder = new BottomBorder();
tblBorders.BottomBorder.Val = new EnumValue(BorderValues.BasicBlackDots);
tblBorders.LeftBorder = new LeftBorder();
tblBorders.LeftBorder.Val = new EnumValue(BorderValues.BasicBlackDots);
tblBorders.RightBorder = new RightBorder();
tblBorders.RightBorder.Val = new EnumValue(BorderValues.BasicBlackDots);
tblBorders.InsideHorizontalBorder = new InsideHorizontalBorder();
tblBorders.InsideHorizontalBorder.Val = BorderValues.Single;
tblBorders.InsideVerticalBorder = new InsideVerticalBorder();
tblBorders.InsideVerticalBorder.Val = BorderValues.Single;
tblPr = new TableProperties();

Create new table row and table cell

//Table Start
TableRow tr = new TableRow();
TableCell tc = new TableCell(AuthorPara);//Paragraph above
TableCellWidth tblCellWidth = new TableCellWidth();
tblCellWidth.Width = 600;
TableCellProperties tcp = new TableCellProperties();
GridSpan griedSpan = new GridSpan();
griedSpan.Val = 11;



In this way table can be created in the open xml using wordprocessingML

1 comment:

  1. Woh !! that's the thing I was looking for. Really very impressive way to play with documents.