English 中文(简体)
Horizontal Text alignment in Word using OpenXml Sdk 2.0
原标题:

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();
问题回答

You should to use w:jc element for your paragraph (w:p) properties (w:pPr) to define your desired horizontal alignment:

<w:tr>
  <w:tc><!-- your table cell -->
    <w:p>
      <w:pPr>
        <w:jc w:val="right"/><!-- horizontal alignment = right -->
      </w:pPr>
      <w:r>
        <w:t>Foo bar</w:t>
      </w:r>
    </w:p>
  </w:tc>
</w:tr>

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.

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

Word.ParagraphProperties ParaProperties = new Word.ParagraphProperties() { ParagraphStyleId = new Word.ParagraphStyleId() { Val = Tbl.GetHashCode().ToString() } };
if (Tbl.HorizontalAlignment == HorizontalAlignment.Center)
     ParaProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Center });
 else if (Tbl.HorizontalAlignment == HorizontalAlignment.Right)
      ParaProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Right });
 else
      ParaProperties.Append(new Word.Justification() { Val = Word.JustificationValues.Left });

This Solved my Problem.. Once again Thanks for Rubens for your help, with which my mistake was Identified.

Here you have three different kind of alignment inside your Stylesheet. You can use this constructor to pass it:

public Stylesheet(params OpenXmlElement[] childElements);

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.

new CellFormats(
                       //VALUE
                       // Index 0 - The default cell style - Alignment left
                       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Left, Vertical = VerticalAlignmentValues.Center }) 
                                     { FontId = 0, FillId = 0, BorderId = 0, ApplyBorder = true },

                       //HEADER
                       // Index 1 - Bold - Green background - align center
                       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }) 
                                     { FontId = 1, FillId = 2, BorderId = 0, ApplyFont = true, ApplyBorder = true, ApplyFill = true },

                       //ERROR HEADER
                       //index 2 - bold text - align center
                       new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }) 
                                     { FontId = 1, FillId = 0, BorderId = 0, ApplyFont = true, ApplyBorder = true, ApplyFill = true }
               )

So, finally you can set your cell as a header in this way:

private enum CellStyleEnum
{
    Value = 0,
    Header = 1,
    Error = 2    
}

var cell = new Cell
{
    DataType = CellValues.InlineString,
    CellReference = header + index,
    StyleIndex = (UInt32)CellStyleEnum.Header
};




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签