3

別の助けが必要です... 私のエクスポート機能は、レポートを Word の表としてエクスポートします。各セルに水平方向の配置プロパティを適用する必要があります。エクスポート用に書いたコードを以下に示します。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();
4

3 に答える 3

8

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 で何かを行う方法を調べることができます。

于 2009-11-25T09:28:33.227 に答える
4

ルーベンス・ファリアス、ありがとう

私の問題はここで解決しました..コードの小さな変更でうまくいきました..問題は、段落プロパティの代わりに実行プロパティの正当化プロパティを与えたことです。

コードを次のように変更しました

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

これで私の問題は解決しました..ルーベンスの助けに感謝します.私の間違いが特定されました.

于 2009-11-25T10:38:14.993 に答える