[英]Remove cell borders in table in Word document (OpenXml.Wordprocessing)
[英]Word OpenXML remove padding inside table
如果您現有的Docx文件中有一個空表,則可能會發現每個Cell
中都有一個空的Paragraph
。 通過使用Append
您可以在空的Paragraph
后面添加新的Paragraph
,這將在單元格的頂部導致一個看起來像填充的空格。
假設您只需要在Cell
中輸入新文本,則可以通過在Cell
(或Table
如果您確定不需要在Table
)上調用RemoveAllChildren
來刪除任何現有的Paragraph
元素, 然后再添加新的Paragraph
:
TableCell cell = body.Descendants<TableCell>().ElementAt(0);
cell.RemoveAllChildren<Paragraph>();
cell.Append(new Paragraph(new Run(new Text(dr["NAME"].ToString()))));
如果這不是問題,那么您可以通過編輯TableCellMargin
來控制填充。 類似於以下內容的東西應該起作用:
if (cell.TableCellProperties != null && cell.TableCellProperties.TableCellMargin != null)
{
cell.TableCellProperties.TableCellMargin.BottomMargin = new BottomMargin() { Width = "0" };
cell.TableCellProperties.TableCellMargin.TopMargin = new TopMargin() { Width = "0" };
}
編輯
完整的代碼清單如下所示:
static void AddDataToTable(string filename)
{
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Open(filename, true))
{
var body = wordDoc.MainDocumentPart.Document.Body;
var paras = body.Elements<TableCell>();
TableCell cell = body.Descendants<TableCell>().ElementAt(0);
cell.RemoveAllChildren<Paragraph>();
cell.Append(new Paragraph(new Run(new Text(dr["NAME"].ToString()))));
if (cell.TableCellProperties != null && cell.TableCellProperties.TableCellMargin != null)
{
cell.TableCellProperties.TableCellMargin.BottomMargin = new BottomMargin() { Width = "0" };
cell.TableCellProperties.TableCellMargin.TopMargin = new TopMargin() { Width = "0" };
}
wordDoc.Close(); // close the template file
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.