我需要其他帮助...我的导出功能将我的报告导出为 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();