C# 在word文檔中復(fù)制表格并粘帖到下一頁(yè)中
object oMissing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
//顯示word文檔
oWord.Visible = true;
//取得word文件模板
object fileName = System.Windows.Forms.Application.StartupPath + "\\word.doc";
//根據(jù)模板生成一個(gè)新文檔,相當(dāng)于另存為
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);
//復(fù)制第一個(gè)表格
oDoc.Tables[1].Select();
oWord.Selection.Copy();
//在這里操作表格中的文本
oDoc.Tables[1].Cell(1, 1).Range.Text = "這是第一個(gè)表格";
//下一頁(yè)
object mymissing = System.Reflection.Missing.Value;
object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWord.Selection.EndKey(ref myunit, ref mymissing);
object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
oWord.Selection.InsertBreak(ref pBreak);
//粘貼第一個(gè)表格
oWord.Selection.Paste();
oDoc.Tables[2].Cell(1, 1).Range.Text = "這是第二個(gè)表格";
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
//顯示word文檔
oWord.Visible = true;
//取得word文件模板
object fileName = System.Windows.Forms.Application.StartupPath + "\\word.doc";
//根據(jù)模板生成一個(gè)新文檔,相當(dāng)于另存為
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);
//復(fù)制第一個(gè)表格
oDoc.Tables[1].Select();
oWord.Selection.Copy();
//在這里操作表格中的文本
oDoc.Tables[1].Cell(1, 1).Range.Text = "這是第一個(gè)表格";
//下一頁(yè)
object mymissing = System.Reflection.Missing.Value;
object myunit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWord.Selection.EndKey(ref myunit, ref mymissing);
object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
oWord.Selection.InsertBreak(ref pBreak);
//粘貼第一個(gè)表格
oWord.Selection.Paste();
oDoc.Tables[2].Cell(1, 1).Range.Text = "這是第二個(gè)表格";
posted on 2008-08-26 22:06 豬 閱讀(2795) 評(píng)論(0) 編輯 收藏