C# 删除Word文档(OpenXml.Wordprocessing)表中的单元格边框
我正在使用C# 删除Word文档(OpenXml.Wordprocessing)表中的单元格边框,c#,openxml,wordprocessingml,C#,Openxml,Wordprocessingml,我正在使用DocumentFormat.OpenXml.Wordprocessing在Word文档中添加一个表。我需要的是删除表最后3行(/N)中前4行(/6)单元格的边框。这些行的添加方式如下: t.Append(new TableRow( new TableCell(new Paragraph(new Run(new Text()))), new TableCell(new Paragraph(new Run(new Text()))), new TableCell(
DocumentFormat.OpenXml.Wordprocessing
在Word文档中添加一个表。我需要的是删除表最后3行(/N)中前4行(/6)单元格的边框。这些行的添加方式如下:
t.Append(new TableRow(
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text()))),
new TableCell(new Paragraph(new Run(new Text("Total:")))),
new TableCell(new Paragraph(new Run(new Text(priceTotal.ToString()))))
));
如何设置表格单元格边框
?我试过以下几种方法:
TableCell cell = new TableCell();
cell.TableCellProperties.TableCellBorders.LeftBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.RightBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.TopBorder.Size.Value = 0;
cell.TableCellProperties.TableCellBorders.BottomBorder.Size.Value = 0;
但是,我尝试的所有操作都返回
System.NullReferenceException
。删除单元格边框的正确方法是什么?您可以在word中创建一个没有边框的表格,如下所示:
public static void CreateTable(string fileName)
{
// Use the file name and path passed in as an argument
// to open an existing Word 2007 document.
using (WordprocessingDocument doc
= WordprocessingDocument.Open(fileName, true))
{
// Create an empty table.
Table table = new Table();
// Create a TableProperties object and specify its border information.
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new BottomBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new LeftBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new RightBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideHorizontalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
},
new InsideVerticalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.None),
}
)
);
// Append the TableProperties object to the empty table.
table.AppendChild<TableProperties>(tblProp);
// Create a row.
TableRow tr = new TableRow();
// Create a cell.
TableCell tc1 = new TableCell();
// Specify the width property of the table cell.
tc1.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
// Specify the table cell content.
tc1.Append(new Paragraph(new Run(new Text("some text"))));
// Append the table cell to the table row.
tr.Append(tc1);
// Create a second table cell by copying the OuterXml value of the first table cell.
TableCell tc2 = new TableCell(tc1.OuterXml);
// Append the table cell to the table row.
tr.Append(tc2);
// Append the table row to the table.
table.Append(tr);
// Append the table to the document.
doc.MainDocumentPart.Document.Body.Append(table);
}
}
公共静态void CreateTable(字符串文件名)
{
//使用传入的文件名和路径作为参数
//打开现有Word 2007文档。
使用(WordprocessingDocument文档)
=WordprocessingDocument.Open(文件名,true))
{
//创建一个空表。
Table Table=新表();
//创建TableProperties对象并指定其边框信息。
TableProperties tblProp=新的TableProperties(
新台边(
新TopBorder()
{
瓦尔=
新枚举值(BorderValues.None),
},
新边界()
{
瓦尔=
新枚举值(BorderValues.None),
},
新LeftBorder()
{
瓦尔=
新枚举值(BorderValues.None),
},
新右边界()
{
瓦尔=
新枚举值(BorderValues.None),
},
新内部水平边界()
{
瓦尔=
新枚举值(BorderValues.None),
},
新的InsideverticalOrder()
{
瓦尔=
新枚举值(BorderValues.None),
}
)
);
//将TableProperties对象追加到空表。
表1.附录儿童(tblProp);
//创建一行。
TableRow tr=新的TableRow();
//创建一个单元格。
TableCell tc1=新的TableCell();
//指定表格单元格的宽度属性。
tc1.追加(新的TableCellProperties(
新的TableCellWidth(){Type=TableWidthUnitValues.Dxa,Width=“2400”});
//指定表格单元格内容。
tc1.追加(新段落(新运行(新文本(“某些文本”)));
//将表格单元格追加到表格行。
tr.Append(tc1);
//通过复制第一个表单元格的OuterXml值来创建第二个表单元格。
TableCell tc2=新的TableCell(tc1.OuterXml);
//将表格单元格追加到表格行。
tr.Append(tc2);
//将表行追加到表中。
表3.追加(tr);
//将表附加到文档中。
doc.MainDocumentPart.Document.Body.Append(表);
}
}
根据您的需要对其进行自定义和优化:)可能是因为边框是在样式表下定义的。有两种方法可以在Word文档中定义表格边框。一种方法是在表格元素中定义边框,另一种方法是在TableCell元素中定义边框。我想为每个TableCell定义边框,因为它们彼此不同。我怎样才能做到呢?实际上,问题是我必须使用
Nil
而不是Null
。Idk为什么,但这就是解决我问题的方法。不过,我会记下你的答案,因为它确实帮助了我,把我推向了正确的方向!谢谢new RightBorder(){Val=new EnumValue(BorderValues.Nil),}