我正在尝试对齐 *.docx-documents
表格单元格中的文本。
一切都很顺利,直到我将 tablecellproperty
附加到 tablecell
本身。
TableCell tc = new TableCell();
TableCellProperties tcpVA = new TableCellProperties();
TableCellVerticalAlignment tcVA= new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center };
tcpVA.Append(tcVA);
tc.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Pct, Width = columnwidths[i] }), tcpVA);
附加cellwidth
、颜色等可以正常工作,但只是TableCellVerticalAlignment
不起作用。
TableCellProperty 的值已设置:
Val = "center"
但在将 TableCellProperties
附加到 tablecell
后,verticalalignment
的属性:
TableCellVerticalAlignment = null
最佳答案
您要向 TableCell
添加两个 TableCellProperties
,一个用于垂直对齐,另一个用于单元格宽度。该架构仅允许一个 TableCellProperties
。
TableCellVerticalAlignment
和 TableCellWidth
都应附加到相同 TableCellProperties
,然后仅附加到该 TableCellProperties
应添加到单元格中。
以下方法是一个示例,它将创建一个带有表格的文档,该表格有一个设置了宽度和对齐属性的单元格以及文本“Hello World!”就在其中。
public static void CreateWordDoc(string filename)
{
using (var wordDocument = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
//add a table, row and column
Table table = body.AppendChild(new Table());
TableRow row = table.AppendChild(new TableRow());
TableCell tc = row.AppendChild(new TableCell());
//create the cell properties
TableCellProperties tcp = new TableCellProperties();
//create the vertial alignment properties
TableCellVerticalAlignment tcVA = new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center };
//create the cell width
TableCellWidth tcW = new TableCellWidth() { Type = TableWidthUnitValues.Pct, Width = "100" };
//append the vertical alignment and cell width objects to the TableCellProperties
tcp.Append(tcW);
tcp.Append(tcVA);
//append ONE TableCellProperties object to the cell
tc.Append(tcp);
//add some text to the cell to test.
Paragraph para = tc.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Hello World!"));
mainPart.Document.Save();
}
}
关于c# - OpenXML ASP.NET c# : tablecell vertical alignment issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30391349/