开放式XML(Microsoft Word):C#表格对齐
开放式XML(Microsoft Word):C#表格对齐,c#,ms-word,openxml,C#,Ms Word,Openxml,我如何在开放XML中的Word文档中间对齐我的表? 这是我的代码,我想在中间对齐表。 这是我的密码。我试图使用tablejustion,但它似乎不起作用 using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document)) { MainDocumentPart mainPart
我如何在开放XML中的Word文档中间对齐我的表? 这是我的代码,我想在中间对齐表。 这是我的密码。我试图使用tablejustion,但它似乎不起作用
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Run run = new Run();
// Goes through all of the forms
foreach (var form in forms)
{
Table table = new Table();
// Initialize all of the table properties
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 },
new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 },
new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 },
new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackSquares), Size = 16 }
)
);
// Align the table to the center
TableJustification justs_center = new TableJustification() { Val = _____ };
table.AppendChild(justs_center);
table.AppendChild<TableProperties>(tblProp);
Paragraph para = body.AppendChild(new Paragraph());
Run run_header = para.AppendChild(new Run());
RunProperties runProps = run_header.AppendChild(new RunProperties(new Bold()));
string username = form.Username;
string proces_header = form.HeaderTitle;
run_header.AppendChild(new Text(proces_header + " | " + username));
for (int i = 0; i < form.FieldList.Count; i++)
{
if (!(form.FieldList[i].Token == "USR" || form.FieldList[i].Token == "SNT"))
{
TableRow tr = new TableRow();
TableCell header_cell = new TableCell();
header_cell.Append(new Paragraph(new Run(new Text(form.FieldList[i].Label + ": " + form.FieldList[i].Value))));
tr.Append(header_cell);
table.Append(tr);
}
}
wordDoc.MainDocumentPart.Document.Body.Append(table);
}
mainPart.Document.Save();
wordDoc.Close();
return "Success";
}
}
使用(WordprocessingDocument wordDoc=WordprocessingDocument.Create(filepath,WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart=wordDoc.AddMainDocumentPart();
mainPart.Document=新文档();
Body Body=mainPart.Document.AppendChild(新Body());
运行=新运行();
//浏览所有表格
foreach(表单中的var表单)
{
Table Table=新表();
//初始化所有表属性
TableProperties tblProp=新的TableProperties(
新台边(
new TopBorder(){Val=new EnumValue(BorderValues.BasicBlackSquares),Size=16},
new LeftBorder(){Val=new EnumValue(BorderValues.BasicBlackSquares),Size=16},
new RightBorder(){Val=new EnumValue(BorderValues.BasicBlackSquares),Size=16},
new BottomBorder(){Val=new EnumValue(BorderValues.BasicBlackSquares),Size=16}
)
);
//将桌子与中心对齐
tablejustization justs_center=新tablejustization(){Val=___};
表2.儿童(justs_中心);
表1.附录儿童(tblProp);
段落段落=正文.附件(新段落());
Run Run_header=para.AppendChild(new Run());
RunProperties runProps=run_header.AppendChild(新的RunProperties(new Bold());
字符串username=form.username;
string proces_header=form.headertile;
运行_header.AppendChild(新文本(proces_header+“|”+用户名));
for(int i=0;i
试图用接受的答案将表格居中似乎不起作用。我还需要对所有行应用中心对齐
var t = new Table();
var tableProps = new TableProperties();
tableProps.TableJustification = new TableJustification { Val = TableRowAlignmentValues.Center };
t.Append(tableProps);
var row = new TableRow();
var rowProps = new TableRowProperties();
rowProps.Append(new TableJustification { Val = TableRowAlignmentValues.Center });
row.Append(rowProps);
使用开放式XML生产力工具发现,试图用接受的答案将表格居中似乎不起作用。我还需要对所有行应用中心对齐
var t = new Table();
var tableProps = new TableProperties();
tableProps.TableJustification = new TableJustification { Val = TableRowAlignmentValues.Center };
t.Append(tableProps);
var row = new TableRow();
var rowProps = new TableRowProperties();
rowProps.Append(new TableJustification { Val = TableRowAlignmentValues.Center });
row.Append(rowProps);
使用Open XML Productivity Tool找到实际上,您只需要对TableRowProperties中的所有行应用中心对齐。即使不在TableProperties中应用对正,该表仍将居中。如果有人知道,我很想知道TableRowProperties中对齐的目的是什么。实际上,您只需要将中心对齐应用于TableRowProperties中的所有行。即使不在TableProperties中应用对正,该表仍将居中。如果有人知道的话,我想知道TableRowProperties中的对正的目的是什么