C#提取Word中的内容并添加到另一人word中
2008-09-21 09:28
417 查看
using System;
using System.Web;
using System.Text;
using System.Data;
using System.Web.UI;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;
namespace test
{
/**//// <summary>
/// WordFile 的摘要说明。
/// </summary>
public class WordFile
{
public WordFile()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/**//// <summary>
/// 生成word文档
/// </summary>
/// <param name="template">模版文件路径</param>
/// <param name="ht">书签与内容</param>
/// <param name="filename">新文件路径</param>
public void CreateWord(string template,object filename)
{
Microsoft.Office.Interop.Word.Application wordApp=new Microsoft.Office.Interop.Word.ApplicationClass();
object missing=Type.Missing;
Microsoft.Office.Interop.Word.Document doc=OpenDoc(template,ref wordApp);
try
{
Microsoft.Office.Interop.Word.Document hdoc=OpenDoc("c://test//test.doc",ref wordApp);
object start=hdoc.Content.Start;
object end=hdoc.Content.End;
Microsoft.Office.Interop.Word.Range rng=hdoc.Range(ref start,ref end);
rng.Select();
Hashtable ht=new Hashtable();
ht.Add("test",rng.Text);
System.Collections.IDictionaryEnumerator et=ht.GetEnumerator();
//doc.Range(ref start,ref end).Select();
//doc.Range rng=doc.Content;
//rng.Select();
/**//*
doc.Paragraphs[1].Range.InsertParagraphBefore();
Microsoft.Office.Tools.Word.Bookmark bookmark1 =
doc.Controls.AddBookmark(doc.Paragraphs[1].Range,
"bookmark1");
string FileName = "C://Sales.doc";
object ConfirmConversions = false;
object Link = false;
object Attachment = false;
bookmark1.InsertFile(FileName, ref missing, ref ConfirmConversions,
ref Link, ref Attachment);
*/
/**///////
while(et.MoveNext())
{
foreach(Microsoft.Office.Interop.Word.Bookmark BM in doc.Bookmarks)
{
object oMissing=System.Reflection.Missing.Value;
if(BM.Name==et.Key.ToString())
{
BM.Select();//查找书签
string[] arr=et.Value.ToString().Split('|');
int i=arr.Length;
if(arr.Length==1)
{
if(et.Value.ToString()=="")
{
BM.Range.Text="无记录";
}
else
{
BM.Range.Text=et.Value.ToString(); //替换书签中的内容
}
}
else //连接图片
{
BM.Range.InlineShapes.AddPicture(arr[1].ToString(),ref oMissing,ref oMissing,ref oMissing);
}
break;
}
}
}
}
catch(System.Exception ee)
{
string ff=ee.ToString();
ff=ff;
}
doc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
doc.Close(ref missing,ref missing,ref missing);
}
/**/////////////////////////////////////////////////////////////////////////////////////
//////////////////内部函数
////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// 返回doc对象
/// </summary>
/// <param name="strDocPath">模版文件路径</param>
/// <param name="wordApp">word应用程序</param>
/// <param name="flag"></param>
/// <returns></returns>
private Microsoft.Office.Interop.Word.Document OpenDoc(string strDocPath,ref Microsoft.Office.Interop.Word.Application wordApp)
{
if(!File.Exists(strDocPath))
{
return null;
}
object fileName=(object)strDocPath;
object isVisible=Type.Missing;
object readOnly=Type.Missing;
object missing=Type.Missing;
wordApp.Visible=false;
Microsoft.Office.Interop.Word.Document doc=null;
try
{
doc=wordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
return doc;
}
catch(System.Exception e)
{
throw new Exception(e.Message);
}
}
}
}
相关文章推荐
- C#提取Word中的内容并添加到另一人word中(http://blog.e-works.net.cn/147018/articles/7340.html)
- c#读取word内容,c#提取word内容
- 能够按页号提取word文档文本内容的小程序,由C#实现
- C#向Word文档中添加内容控件的方法示例
- 能够按页号提取word文档文本内容的小程序,由C#实现
- C# 给Word文档添加内容控件
- C# 给Word文档添加内容控件
- c#读取word内容,c#提取word内容
- c#读取word内容,c#提取word内容
- C#操作Word总结(二)——设置文档格式并添加文本内容、超链接、添加图片
- C#采用OpenXml给Word文档添加表格
- C# 发送邮件(内容中可以插入图片,邮件可以添加附件,163邮箱、QQ邮箱等都可以使用)
- C#替换Word中的文本内容
- c#.net 将网页内容转换成word或excel文档
- 提取网页内容存储为word的方法
- C# WINFORM中的combobox.items.add实现像web开发那样,添加显示内容text和实际value值
- c#设置xml内容不换行及属性xsi:nil=true的空节点添加
- java提取word内容
- C# 之 比较两个word文档的内容
- C#/VB.NET 给Word文档添加/撤销书签