使用c#以编程方式设置word文档表格单元格边距

时间:2014-07-20 19:37:08

标签: c# ms-word office-interop

我们可以使用此步骤更改MS WORD中的defualt表格单元格边距

Click the table.
On the Table menu, click Table Properties, and then click the Table tab.
Click Options.
Under Default cell margins, enter the new values you want.

而且我想知道如何以编程方式执行此操作我已尝试top and bottom padding属性但它不起作用我也尝试了spacing但是我没有工作也是如此有没有办法设置使用Microsoft.Interop.Word库的默认单元格边距非常感谢

PS: 我正在使用Microsoft.Interop.Word在页眉和页脚中添加表格,这完全可以预期:/

1 个答案:

答案 0 :(得分:4)

有表填充,还有Cell填充。我猜测表填充包含要应用于添加到表中的新单元格的默认值。可能使用Cell填充来更改现有单元格。 E.g。

    Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
    Documents docs = app.Documents;
    Document doc = docs.Open("C:\\temp\\Test2.docx", ReadOnly:true);
    Table t = doc.Tables[1];
    double b1 = t.BottomPadding;
    double t1 = t.TopPadding;
    double r1 = t.RightPadding;
    double l1 = t.LeftPadding;

    Range r = t.Range;
    Cells cells = r.Cells;
    for (int i = 1; i <= cells.Count; i++) {
        Cell cell = cells[i];
        double b2 = cell.BottomPadding;
        double t2 = cell.TopPadding;
        double r2 = cell.RightPadding;
        double l2 = cell.LeftPadding;

        // e.g. Here is the edit:
        cell.TopPadding = 21.6f;
        cell.BottomPadding = 28.8f;

        Range r2b = cell.Range;
        String txt = r2b.Text;
        Marshal.ReleaseComObject(cell);
        Marshal.ReleaseComObject(r2b);
    }

    doc.Close(false);
    app.Quit(false);
    Marshal.ReleaseComObject(cells);
    Marshal.ReleaseComObject(r);
    Marshal.ReleaseComObject(t);
    Marshal.ReleaseComObject(doc);
    Marshal.ReleaseComObject(docs);
    Marshal.ReleaseComObject(app);