天天看點

VS操作Word模闆中的表格

1、利用Word文檔模闆建立Word文檔

    object filedot = "....//Member.dot";

    Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

    Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref filedot, ref Nothing, ref Nothing, ref Nothing);

    WordApp.Visible = true;

2、擷取表格,填充内容

    Microsoft.Office.Interop.Word.Selection selection=WordApp.Selection; 

    Microsoft.Office.Interop.Word.Tables newTables = WordDoc.Tables;

    Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables[1];//得到表1

3、移動光标

    object count = 1;

    object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;

    WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//左、右、上移類似

4、在表格中插入行

    WordApp.Selection.InsertRowsBelow(ref count);

5、在表格中填充内容

     newTable.Cell(row, col).Range.Text="";

6、擷取表格中的内容

     string s = newTable.Cell(row, col).Range.Text;

7、檔案儲存

     object filename = "....//儲存.doc";  //檔案儲存路徑和檔案名

     WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);