C# 打开XML MS Word表格标题行高文本方向从下到上从左到右
我正在使用OpenXMLSDK2.5在Word文档中插入表。我目前遇到的问题是表格标题的行高。它适用于从左到右从上到下LTTB的正常文本方向。但是,只要我将文本方向设置为从下到上从左到右BTLR,我的标题行就不会调整以适应单元格内容 代码如下C# 打开XML MS Word表格标题行高文本方向从下到上从左到右,c#,openxml,C#,Openxml,我正在使用OpenXMLSDK2.5在Word文档中插入表。我目前遇到的问题是表格标题的行高。它适用于从左到右从上到下LTTB的正常文本方向。但是,只要我将文本方向设置为从下到上从左到右BTLR,我的标题行就不会调整以适应单元格内容 代码如下 void InsertTable(string[,] tableData, int numberOfRows, int numberOfColumns, string locationInDocument, string textDirectionHead
void InsertTable(string[,] tableData, int numberOfRows, int numberOfColumns, string locationInDocument, string textDirectionHeadings)
{
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(_newDocument, true))
{
var docPart = myDoc.MainDocumentPart;
var doc = docPart.Document;
var table = new Table();
var tableBorderTop = new TopBorder();
var tableBorderBottom = new BottomBorder();
var tableBorderLeft = new LeftBorder();
var tableBorderRight = new RightBorder();
var tableBorderHorizontal = new InsideHorizontalBorder();
var tableBorderVertical = new InsideVerticalBorder();
var tableProperties = new TableProperties();
var borders = new TableBorders();
// Set Border Styles for Table
tableBorderTop.Val = BorderValues.Single;
tableBorderTop.Size = 6;
tableBorderBottom.Val = BorderValues.Single;
tableBorderBottom.Size = 6;
tableBorderLeft.Val = BorderValues.Single;
tableBorderLeft.Size = 6;
tableBorderRight.Val = BorderValues.Single;
tableBorderRight.Size = 6;
tableBorderHorizontal.Val = BorderValues.Single;
tableBorderHorizontal.Size = 6;
tableBorderVertical.Val = BorderValues.Single;
tableBorderVertical.Size = 6;
// Assign Border Styles to Table Borders
borders.TopBorder = tableBorderTop;
borders.BottomBorder = tableBorderBottom;
borders.LeftBorder = tableBorderLeft;
borders.RightBorder = tableBorderRight;
borders.InsideHorizontalBorder = tableBorderHorizontal;
borders.InsideVerticalBorder = tableBorderVertical;
// Append Border Styles to Table Properties
tableProperties.Append(borders);
// Assign Table Properties to Table
table.Append(tableProperties);
//Adds the Table Headings for each Column
var tableRowHeader = new TableRow();
tableRowHeader.Append(new TableRowHeight() { HeightType = HeightRuleValues.Auto });
for (int i = 0; i < numberOfColumns; i++)
{
var tableCellHeader = new TableCell();
//Assign Font Properties to Run
var runPropHeader = new RunProperties();
runPropHeader.Append(new Bold());
runPropHeader.Append(new Color() { Val = "000000" });
//Create New Run
var runHeader = new Run();
//Assign Font Properties to Run
runHeader.Append(runPropHeader);
var columnHeader = new Text();
//Assign the Pay Rule Name to the Run
columnHeader = new Text(tableData[0, i]);
runHeader.Append(columnHeader);
//Create Properties for Paragraph
var justificationHeader = new Justification();
justificationHeader.Val = JustificationValues.Left;
var paraPropsHeader = new ParagraphProperties(justificationHeader);
SpacingBetweenLines spacing = new SpacingBetweenLines() { Line = "240", LineRule = LineSpacingRuleValues.Auto, Before = "0", After = "0" };
paraPropsHeader.Append(spacing);
var paragraphHeader = new Paragraph();
paragraphHeader.Append(paraPropsHeader);
paragraphHeader.Append(runHeader);
tableCellHeader.Append(paragraphHeader);
var tableCellPropertiesHeader = new TableCellProperties();
var tableCellWidthHeader = new TableCellWidth();
tableCellPropertiesHeader.Append(new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "#C0C0C0" });
var textDirectionHeader = new TextDirection();
if (textDirectionHeadings == "BTLR")
{
textDirectionHeader.Val = TextDirectionValues.BottomToTopLeftToRight;
}
if (textDirectionHeadings == "LRTB")
{
textDirectionHeader.Val = TextDirectionValues.LefToRightTopToBottom;
}
tableCellPropertiesHeader.Append(textDirectionHeader);
tableCellWidthHeader.Type = TableWidthUnitValues.Auto;
tableCellPropertiesHeader.Append(tableCellWidthHeader);
tableCellHeader.Append(tableCellPropertiesHeader);
tableRowHeader.Append(tableCellHeader);
}
tableRowHeader.AppendChild(new TableHeader());
table.Append(tableRowHeader);
//Create New Row in Table for Each Record
int r = 1;
for (int a = 0; a < (numberOfRows - 1); a++)
{
var tableRow = new TableRow();
for (int i = 0; i < numberOfColumns; i++)
{
var propertyText = tableData[r, i];
var tableCell = new TableCell();
//Assign Font Properties to Run
var runProp = new RunProperties();
runProp.Append(new Bold());
runProp.Append(new Color() { Val = "000000" });
//Create New Run
var run = new Run();
//Assign Font Properties to Run
run.Append(runProp);
//Assign the text to the Run
var text = new Text(propertyText);
run.Append(text);
//Create Properties for Paragraph
var justification = new Justification();
justification.Val = JustificationValues.Left;
var paraProps = new ParagraphProperties(justification);
var paragraph = new Paragraph();
paragraph.Append(paraProps);
paragraph.Append(run);
tableCell.Append(paragraph);
var tableCellProperties = new TableCellProperties();
var tableCellWidth = new TableCellWidth();
tableCellWidth.Type = TableWidthUnitValues.Auto;
tableCellProperties.Append(tableCellWidth);
tableCell.Append(tableCellProperties);
tableRow.Append(tableCell);
}
r = r + 1;
table.Append(tableRow);
};
var res = from bm in docPart.Document.Body.Descendants<BookmarkStart>()
where bm.Name == locationInDocument
select bm;
var bookmark = res.SingleOrDefault();
var parent = bookmark.Parent; // bookmark's parent element
Paragraph newParagraph = new Paragraph();
parent.InsertAfterSelf(newParagraph);
if (bookmark != null)
{
newParagraph.InsertBeforeSelf(table);
}
}
}
void InsertTable(字符串[,]表数据,int numberOfRows,int numberOfColumns,字符串位置文档,字符串文本方向标题)
{
使用(WordprocessingDocument myDoc=WordprocessingDocument.Open(_newDocument,true))
{
var docPart=myDoc.MainDocumentPart;
var doc=docPart.Document;
var table=新表();
var tableBorderTop=新的TopBorder();
var tableBorderBottom=新的BottomBorder();
var tableBorderLeft=新的LeftBorder();
var tableBorderRight=新的RightBorder();
var TableBorderHorizontalBorder=新的InsideHorizontalBorder();
var tableBorderVertical=新的InsideverticalOrder();
var tableProperties=新tableProperties();
var borders=新的TableBorders();
//设置表格的边框样式
tableBorderTop.Val=BorderValues.Single;
桌面。尺寸=6;
tableBorderBottom.Val=BorderValues.Single;
桌面边框底部。大小=6;
tableBorderLeft.Val=BorderValues.Single;
表边框左侧。大小=6;
tableBorderRight.Val=BorderValues.Single;
右边的表格边框。大小=6;
tableBorderHorizontal.Val=BorderValues.Single;
表格边框水平。大小=6;
tableBorderVertical.Val=BorderValues.Single;
表格边框垂直。大小=6;
//将边框样式指定给表格边框
borders.TopBorder=桌面边框;
borders.BottomBorder=表格边框底部;
borders.LeftBorder=tableBorderLeft;
borders.RightBorder=tableBorderRight;
borders.InsideHorizontalBorder=表格边框水平;
borders.InsideVerticalBorder=tableBorderVertical;
//将边框样式附加到表属性
tableProperties.Append(边框);
//将表属性指定给表
table.Append(tableProperties);
//为每列添加表标题
var tableRowHeader=新TableRow();
tableRowHeader.Append(新的TableRowHeight(){HeightType=HeightRuleValues.Auto});
for(int i=0;i