在Open XML Word文档中并排创建两个表

时间:2014-06-26 14:58:20

标签: c# .net openxml openxml-sdk

我可以轻松地创建两个表,但是我很难让它们像这样并排显示:

Tables Side-by-Side

我不确定如何使用Open XML SDK实现这一目标。我猜它会是一个TableProperty或一个段落的技巧,但使用生产力工具,我无法解决它。代码段:

int LeftWidth = 2000;
int RightWidth = 2000;
int NumberOfCols = 2;
Table leftTable = StartTable(NumberOfCols, LeftWidth); // Create a basic table
Table rightTable = StartTable(NumberOfCols, RightWidth);

body.Append(leftTable);

/// Do something to right table properties here?

body.Append(rightTable);

我对不同的方法持开放态度,虽然理想情况下这个想法也可以并排转移到三个表格。

1 个答案:

答案 0 :(得分:0)

最后,我意识到有两种主要方法可以实现这一点 - 单击表格并将其左上角十字线拖到您想要的位置,或者将页面拆分为两列并放置一个列分隔符表之间。

浮动表方法

int LeftWidth = 2000;
int RightWidth = 2000;
int NumberOfCols = 2;
Table leftTable = StartTable(NumberOfCols, LeftWidth); // Create a basic table
Table rightTable = StartTable(NumberOfCols, RightWidth);

/// Add table position properties and place table in top left
TableProperties tblProps = leftTable.Descendants<TableProperties>().First();
TablePositionProperties tblPos = new TablePositionProperties() { VerticalAnchor = VerticalAnchorValues.Text, TablePositionY = 1 };
TableOverlap overlap = new TableOverlap() { Val = TableOverlapValues.Overlap };
tblProps.Append(tblPos, overlap);

body.Append(leftTable);

/// Add position property to right table, set 8700 and 2400 to where you want the table
TableProperties tblProps2 = rightTable.Descendants<TableProperties>().First();
TablePositionProperties tblPos2 = new TablePositionProperties() { HorizontalAnchor = HorizontalAnchorValues.Page, VerticalAnchor = VerticalAnchorValues.Page, TablePositionX = 8700, TablePositionY = 2400 };
TableOverlap overlap2 = new TableOverlap() { Val = TableOverlapValues.Overlap };

tblProps2.Append(tblPos2, overlap2);

body.Append(rightTable);

列方法

在表格放置此代码之前,将先前的内容保留在一列中:

/// Make sure everything else stays at one column
Paragraph oneColPara = body.AppendChild(new Paragraph());

/// Adjust current doc properties to keep things like landscape
SectionProperties sectionOneProps = null;
if (body.Descendants<SectionProperties>().Count() > 0)
  sectionOneProps = (SectionProperties)body.Descendants<SectionProperties>().First().CloneNode(true);
else
  sectionOneProps = new SectionProperties();

sectionOneProps.RemoveAllChildren<Columns>();
sectionOneProps.RemoveAllChildren<DocGrid>();

sectionOneProps.Append(new Columns(){ Space = "708" }, new DocGrid(){ LinePitch = 360 }, new SectionType { Val = SectionMarkValues.Continuous } );
oneColPara.Append(new ParagraphProperties(sectionOneProps));

然后在表格部分中执行此操作:

int LeftWidth = 2000;
int RightWidth = 2000;
int NumberOfCols = 2;
Table leftTable = StartTable(NumberOfCols, LeftWidth); // Create a basic table
Table rightTable = StartTable(NumberOfCols, RightWidth);

/// Need this blank para to line tables up
body.Append(new Paragraph());

body.Append(leftTable);

/// Place the tables side by side
body.Append(new Paragraph(new Run(new Break() { Type = BreakValues.Column })));

body.Append(rightTable);

/// Make section have 2 columns
Paragraph twoColPara = body.AppendChild(new Paragraph());

/// Adjust current doc properties to keep things like landscape
SectionProperties sectionTwoProps = null;
if (body.Descendants<SectionProperties>().Count() > 0)
  sectionTwoProps = (SectionProperties)body.Descendants<SectionProperties>().First().CloneNode(true);
else
  sectionTwoProps = new SectionProperties();

sectionTwoProps.RemoveAllChildren<Columns>();
sectionTwoProps.RemoveAllChildren<DocGrid>();

sectionTwoProps.Append(new Columns() { Space = "284", ColumnCount = 2 }, new DocGrid() { LinePitch = 360 }, new SectionType { Val = SectionMarkValues.Continuous });
twoColPara.Append(new ParagraphProperties(sectionTwoProps));