I need another help... My export function exports my report as a table in word. I need to apply horizontal alignment property for each cell. The Code I wrote for exporting is given below. Tbl is a textblock which I am using in my report. I wrote Alignment code here. But doesn't works.. Please help me to accomplish this task using OpenXML SDk 2.0
using Word = DocumentFormat.OpenXml.Wordprocessing;
WordprocessingDocument WordDoc = WordprocessingDocument.Create(SavePath, WordprocessingDocumentType.Document);
MainDocumentPart mainDocument = WordDoc.AddMainDocumentPart();
mainDocument.Document = new Word.Document();
StyleDefinitionsPart StylesDefs = mainDocument.AddNewPart<StyleDefinitionsPart>();
StylesDefs.Styles = new Word.Styles();
Word.Body body = new Word.Body();
Word.Table WordTable = new Word.Table();
Word.TableRow Row;
Word.TableCell Cell = new Word.TableCell();
Word.Style ParaStyle = new Word.Style(new Word.Name() { Val = Tbl.GetHashCode().ToString() });
Word.RunProperties ParaRunProperties = new Word.RunProperties();
ParaRunProperties.Append(new Word.RunFonts() { Ascii = Tbl.FontFamily.ToString() });
if (Tbl.HorizontalAlignment == HorizontalAlignment.Center)
ParaRunProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Center });
else if (Tbl.HorizontalAlignment == HorizontalAlignment.Right)
ParaRunProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Right });
else
ParaRunProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Left });
ParaStyle.Append(ParaRunProperties);
StylesDefs.Styles.Append(ParaStyle);
Word.ParagraphProperties ParaProperties = new Word.ParagraphProperties() { ParagraphStyleId = new Word.ParagraphStyleId() { Val = Tbl.GetHashCode().ToString() } };
Cell.Append(new Word.Paragraph(ParaProperties, new Word.Run(new Word.Text(Tbl.Text))));
Row.Append(Cell);
WordTable.Append(Row);
body.Append(WordTable);
mainDocument.Document.Append(body);
mainDocument.Document.Save();
WordDoc.Close();
Thanks Rubens Farias,
My problem solved here.. Small Change in Code made it work.. Problem was I gave Justification property for Run Properties Instead Paragraph Properties.
I Changed Code as
This Solved my Problem.. Once again Thanks for Rubens for your help, with which my mistake was Identified.
You should to use
w:jc
element for your paragraph (w:p
) properties (w:pPr
) to define your desired horizontal alignment:You always can to save a Word document as OpenXML, rename it to .zip and unpack it to inspect how to do something in OpenXML.
Here you have three different kind of alignment inside your Stylesheet. You can use this constructor to pass it:
This is possible because CellFormats inherits from OpenXmlElement. In my code, I have created my own fill, font and border... don't pay attention to this if you don't need it.
So, finally you can set your cell as a header in this way: