簡體   English   中英

使用OpenXml Sdk 2.0在Word中進行水平文本對齊

[英]Horizontal Text alignment in Word using OpenXml Sdk 2.0

我需要另一個幫助...我的導出功能將我的報告導出為一個表格。 我需要為每個單元格應用水平對齊屬性。 我寫的出口代碼如下。 Tbl是我在報告中使用的文本塊。 我在這里寫了對齊代碼。 但是不起作用..請幫助我使用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();

您應該使用w:jc元素作為段落( w:p )屬性( w:pPr )來定義所需的水平對齊:

<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>

您始終可以將Word文檔另存為OpenXML,將其重命名為.zip並將其解壓縮以檢查如何在OpenXML中執行某些操作。

謝謝Rubens Farias,

我的問題在這里解決了。代碼中的小變化使它工作。問題是我為運行屬性而不是段落屬性提供了對齊屬性。

我將代碼更改為

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 });

這解決了我的問題..再次感謝魯本斯的幫助,我的錯誤被識別出來了。

在這里, 樣式表中有三種不同的對齊方式。 您可以使用此構造函數來傳遞它:

public Stylesheet(params OpenXmlElement[] childElements);

這是可能的,因為CellFormats繼承自OpenXmlElement 在我的代碼中,我創建了自己的填充,字體和邊框...如果您不需要它,請不要注意這一點。

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 }
               )

因此,最后您可以通過以下方式將單元格設置為標題:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM