本文主要是介绍openxml 给word添加table,复制样式和字体,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// 设置表格边框
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 },
new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 },
new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 },
new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 },
new InsideHorizontalBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 },
new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.None), Size = 0 }
)
);
Table table = new Table();
TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };
table.Append(tblProp);
string a = "100", b = "2000", c = "200", d = "2500";
for (int i = 0; i < costList.Length; i++)
{
if (costList[i] != "")
{
RunFonts rf = r.RunProperties.RunFonts;
//DocumentFormat.OpenXml
DocumentFormat.OpenXml.Wordprocessing.FontSize fs = r.RunProperties.FontSize;
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = a } };
// cell.TableCellProperties = new TableCellProperties() { TableCellMargin = new TableCellMargin() { RightMargin = new RightMargin() { Type = TableWidthUnitValues.Dxa, Width = "1000" } } };
cell.Append(new Paragraph(new Run(new Text(""))));
row.Append(cell);
cell = new TableCell();
cell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = b } };
//cell.TableCellProperties = new TableCellProperties() { TableCellMargin = new TableCellMargin() { RightMargin = new RightMargin() { Type = TableWidthUnitValues.Dxa, Width = "2000" } } };
Run rn = new Run(new Text(“”));
rn.RunProperties = new RunProperties();
rn.RunProperties.RunFonts = new RunFonts { Ascii = rf.Ascii };
rn.RunProperties.FontSize = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
rn.RunProperties.FontSize.Val = fs.Val;
cell.Append(new Paragraph(new Run(rn)));
row.Append(cell);
cell = new TableCell();
//设置cell的宽度
cell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = c } };
// cell.TableCellProperties = new TableCellProperties() { TableCellMargin = new TableCellMargin() { RightMargin = new RightMargin() { Type = TableWidthUnitValues.Dxa, Width = "1000" } } };
rn = new Run(new Text(":"));
cell.Append(new Paragraph(new Run(rn)));
row.Append(cell);
cell = new TableCell();
//cell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "100" } };
cell.TableCellProperties = new TableCellProperties() { TableCellMargin = new TableCellMargin() { RightMargin = new RightMargin() { Type = TableWidthUnitValues.Dxa, Width = a } } };
rn = new Run(new Text("$"));
if ( i == costList.Length - 1)
{
//设置cell双下划线,单下划线为:Signle
cell.TableCellProperties.TableCellBorders = new TableCellBorders() { BottomBorder = new BottomBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Double } };
}
cell.Append(new Paragraph(new Run(rn)));
row.Append(cell);
cell = new TableCell();
cell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = d } };
rn = new Run(new Text(amt));
rn.RunProperties = new RunProperties();
if (i == costList.Length - 1)
{
//给table的cell 添加下划线
//cell.TableCellProperties = new TableCellProperties() { TableCellBorders = new TableCellBorders() { BottomBorder = new BottomBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Single } } };
cell.TableCellProperties.TableCellBorders = new TableCellBorders() { BottomBorder = new BottomBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Double } };
}
//cell.TableCellProperties.TableCellMargin = new TableCellMargin();
//cell.TableCellProperties.TableCellMargin.RightMargin = new RightMargin();
//内容靠右
ParagraphProperties paragraphProperties = new ParagraphProperties();
paragraphProperties.AppendChild<Justification>(new Justification() { Val = JustificationValues.Right });
cell.TableCellProperties.AppendChild<ParagraphProperties>(paragraphProperties);
//内容加粗
rn.RunProperties.Bold = new Bold { Val = new OnOffValue() { Value = true } };
//字体
rn.RunProperties.RunFonts = new RunFonts { Ascii = rf.Ascii };
rn.RunProperties.FontSize = new DocumentFormat.OpenXml.Wordprocessing.FontSize();
rn.RunProperties.FontSize.Val = fs.Val;
cell.Append(new Paragraph(rn));
row.Append(cell);
table.Append(row);
}
}
//Table banktable = new Table();
//TableRow bankRow = new TableRow();
//TableCell bankCell = new TableCell();
//bankCell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = a } };
//bankCell.Append(new Paragraph(new Run(new Text(""))));
//bankRow.Append(bankCell);
//bankCell = new TableCell();
//bankCell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = b } };
//bankCell.Append(new Paragraph(new Run(new Text(""))));
//bankRow.Append(bankCell);
//bankCell = new TableCell();
//bankCell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = c } };
//bankCell.Append(new Paragraph(new Run(new Text(""))));
//bankCell.TableCellProperties.TableCellBorders = new TableCellBorders() { TopBorder = new TopBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Single } };
//bankRow.Append(bankCell);
//bankCell = new TableCell();
//bankCell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = a } };
//bankCell.Append(new Paragraph(new Run(new Text(""))));
//bankCell.TableCellProperties.TableCellBorders = new TableCellBorders() { TopBorder = new TopBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Single } };
//bankRow.Append(bankCell);
//bankCell = new TableCell();
//bankCell.TableCellProperties = new TableCellProperties() { TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = d } };
//bankCell.Append(new Paragraph(new Run(new Text(""))));
//bankCell.TableCellProperties.TableCellBorders = new TableCellBorders() { BottomBorder = new BottomBorder() { Val = DocumentFormat.OpenXml.Wordprocessing.BorderValues.Single } };
//TableRowHeight trh = new TableRowHeight();
//trh.Val = 1;
//bankRow.Append(trh);
//bankRow.Append(bankCell);
//banktable.Append(bankRow);
//
//table.Append(banktable);
/
r.Elements<Text>().FirstOrDefault().Text = "";
r.AppendChild(new Paragraph(new Run(table)));
这篇关于openxml 给word添加table,复制样式和字体的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!