【问题标题】:Word table with borders带边框的单词表
【发布时间】:2013-05-24 10:05:41
【问题描述】:

以下代码将表格插入到 Word 文档中:

using Word = Microsoft.Office.Interop.Word;
...
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc";
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

Word.Table oTable;
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
oTable = oDoc.Tables.Add(wrdRng, 3, 5, ref oMissing, ref oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = 6;
int r, c;
string strText;
for (r = 1; r <= 3; r++)
    for (c = 1; c <= 5; c++)
    {
        strText = "r" + r + "c" + c;
        oTable.Cell(r, c).Range.Text = strText;
    }

此代码来自文章:http://support.microsoft.com/Default.aspx?scid=kb;en-us;316384&spid=2530&sid=47

结果是:

r1c1 r1c2 r1c3 r1c4 r1c5 r2c1 r2c2 r2c3 r2c4 r2c5 r3c1 r3c2 r3c3 r3c4 r3c5

生成的表格没有边框。如何更改此代码以获取带有边框的表格,例如在“插入表格”Word 命令中?

【问题讨论】:

    标签: c# automation ms-word ms-office


    【解决方案1】:

    您可以尝试使用以下命令:

    oTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; 
    oTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleDouble;
    

    循环使用它们。放文字后。

    【讨论】:

    • 谢谢。这段代码,无论是添加到循环还是循环之前/之后,都会创建水平线和外部垂直线。内部垂直线仍然缺失。
    【解决方案2】:

    试试这个变种:

    oTable.Cell(r, c).Range.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;
    oTable.Cell(r, c).Range.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;
    oTable.Cell(r, c).Range.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;
    oTable.Cell(r, c).Range.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;
    

    放入循环。

    你也可以设置线宽和颜色:

    oTable.Cell(r, c).Range.Borders[WdBorderType.wdBorderBottom].LineWidth = WdLineWidth.wdLineWidth050pt;
    oTable.Cell(r, c).Range.Borders[WdBorderType.wdBorderBottom].Color = WdColor.wdColorRed;
    

    【讨论】:

      【解决方案3】:

      我使用以下选项设置所有边框,这比为每个单元格设置边框要容易得多。请注意,获取Range rng 的逻辑是针对 VSTO 插件的,但表逻辑保持不变。

          using Microsoft.Office.Interop.Word;
          ......
          void AddTable()
          {
            Range rng = Globals.ThisAddIn.Application.Selection.Range;
            Table tbl = rng.Tables.Add(rng, 3, 5); //make table at current selection
            tbl.Range.Font.Name = "Calibri";
            tbl.Range.Font.Size = 10.5F;
            tbl.Range.Font.Bold = 0;
      
            //these 2 lines put borders both inside & outside - see result image at end
            tbl.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
            tbl.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
      
            for (r = 1; r <= 3; r++)
              for (c = 1; c <= 5; c++)
              {
                strText = "r" + r + "c" + c;
                oTable.Cell(r, c).Range.Text = strText;
              }
      
          }
      

      结果将如下表所示,带有边框

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2020-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多