3

次のコードは、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 コマンドのように、このコードを変更して罫線付きの表を取得するにはどうすればよいですか?

4

3 に答える 3

11

このバリアントを試してください:

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;
于 2013-05-29T09:40:46.523 に答える
1

次のコマンドを試すことができます。

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

サイクルで使用してください。テキストを入れた後。

于 2013-05-29T08:37:22.947 に答える