Создайте две параллельные таблицы в документе Open XML Word

#c# #.net #openxml #openxml-sdk

#c# #.net #openxml #openxml-sdk

Вопрос:

Я могу создать две таблицы достаточно просто, но у меня возникают проблемы с тем, чтобы они отображались рядом, как это:

Параллельные таблицы

Я не уверен, как добиться этого с помощью Open XML SDK. Я предполагаю, что это будет либо свойство таблицы, либо трюк с абзацами, но с помощью инструмента повышения производительности я не смог его решить. Фрагмент кода:

 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:

В конце концов я понял, что есть два основных способа добиться этого в Word — либо щелкнуть таблицу и перетащить ее верхнее левое перекрестие туда, где вы хотите, либо разделить страницу на два столбца и поместить разрыв столбца между таблицами.

Метод плавающих таблиц

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

Комментарии:

1. или третий способ: вложите свои таблицы в другую таблицу.