Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用OpenXMLSDK2.0在Word中进行水平文本对齐_C#_Ms Word_Openxml - Fatal编程技术网

C# 使用OpenXMLSDK2.0在Word中进行水平文本对齐

C# 使用OpenXMLSDK2.0在Word中进行水平文本对齐,c#,ms-word,openxml,C#,Ms Word,Openxml,我需要另一个帮助。。。我的导出功能将我的报表导出为word中的表格。 我需要为每个单元格应用水平对齐属性。下面给出了我为导出而编写的代码。Tbl是我在报告中使用的文本块。 我在这里写了对齐代码。但是不起作用。。请帮助我使用OpenXMLSDK2.0完成此任务 using Word = DocumentFormat.OpenXml.Wordprocessing; WordprocessingDocument WordDoc = WordprocessingDocument.Create(Sa

我需要另一个帮助。。。我的导出功能将我的报表导出为word中的表格。 我需要为每个单元格应用水平对齐属性。下面给出了我为导出而编写的代码。Tbl是我在报告中使用的文本块。 我在这里写了对齐代码。但是不起作用。。请帮助我使用OpenXMLSDK2.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();
使用Word=DocumentFormat.OpenXml.Wordprocessing;
WordprocessingDocument WordDoc=WordprocessingDocument.Create(保存路径,WordprocessingDocumentType.Document);
MainDocumentPart mainDocument=WordDoc.AddMainDocumentPart();
mainDocument.Document=新单词.Document();
StyleDefinitionsPart StylesDefs=mainDocument.AddNewPart();
StylesDefs.Styles=新单词.Styles();
Body=新单词.Body();
Word.Table WordTable=新单词.Table();
Word.TableRow行;
Word.TableCell Cell=新单词.TableCell();
Word.Style ParaStyle=new Word.Style(new Word.Name(){Val=Tbl.GetHashCode().ToString()});
Word.RunProperties ParaRunProperties=新单词.RunProperties();
ParaRunProperties.Append(new Word.RunFonts(){Ascii=Tbl.FontFamily.ToString()});
如果(Tbl.HorizontalAlignment==HorizontalAlignment.Center)
ParaRunProperties.Append(new Word.Justification(){Val=Word.justicationvalues.Center});
else if(Tbl.HorizontalAlignment==HorizontalAlignment.Right)
ParaRunProperties.Append(new Word.Justification(){Val=Word.justicationvalues.Right});
其他的
ParaRunProperties.Append(new Word.Justification(){Val=Word.justicationvalues.Left});
ParaStyle.Append(ParaRunProperties);
StylesDefs.Styles.Append(ParaStyle);
Word.ParagraphProperties ParaProperties=new Word.ParagraphProperties(){ParagraphStyleId=new Word.ParagraphStyleId(){Val=Tbl.GetHashCode().ToString()};
Cell.Append(新词.段落(准属性,新词.运行(新词.文本(Tbl.Text)));
行。追加(单元格);
追加(行);
body.Append(WordTable);
mainDocument.Document.Append(body);
mainDocument.Document.Save();
WordDoc.Close();

您应该为段落(
w:p
)属性(
w:pPr
)使用
w:jc
元素来定义所需的水平对齐方式:

<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

我的问题在这里解决了。。代码中的小改动使它工作了。。问题是我为运行属性而不是段落属性提供了justify属性

我将代码更改为

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