您的位置:首页 > 编程语言 > C#

c#将一个word中的内容放到另一个word的表格中

2013-06-07 09:59 309 查看
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
/// <summary>
/// 将一个word中的内容拷贝到另外一个word中的表格中
/// 前提是目标word中有一个表格
/// </summary>
/// <param name="source">源文件</param>
/// <param name="target">目标文件</param>
/// <param name="row">行</param>
/// <param name="col">列</param>
public void WordCopy2WordTable(string source,string target,int row,int col)
{
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc ,oTarget;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = source;
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
oDoc.ActiveWindow.Selection.WholeStory();
oDoc.ActiveWindow.Selection.Copy();
oDoc.Close();

object targetfile = target;
oTarget = oWord.Documents.Open(ref targetfile,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
Word.Table table = oTarget.Tables[1];
table.Cell(row, col).Range.Paste();
oTarget.Save();
oTarget.Close();
object savechange = true;
oWord.Quit(savechange, oMissing, oMissing);

}


适合执行一次,多次的话 需要修改中间开关文档,需要配置com组件,具体配置
http://www.cnblogs.com/mengxingxinqing/archive/2013/06/07/3123344.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐