zoukankan
html css js c++ java
C#提取Word中的内容并添加到另一人word中(http://blog.eworks.net.cn/147018/articles/7340.html)
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);
}
}
}
}
查看全文
相关阅读:
jquery mobile (一)
jquery mobile 前言
MD5Helper辅助类
设计权限管理系统(十四)
设计权限管理系统(十三)
display:table-cell的min-height
CORS跨域cookie传递
性能优化-合成层
textarea自适应高度
仿今日头条按钮loading效果
原文地址:https://www.cnblogs.com/sunheyubo/p/995254.html
最新文章
Listview性能优化
【第三篇】Volley图片加载之NetworkImageView代码分析
设计模式--观察者设计模式
android 进程和线程管理
【界面优化】使用viewpagerindicator添加下划线滑动动画
RIDE指定log和report的输出目录
用命令行执行ROBOT FRAMEWORK用例
linux大全链接
ch6-定制数据对象(打包代码和数据)
python常用BIF汇总
热门文章
Eclipse+PyDev搭建Python开发环境(Windows篇)
ch5-处理数据,抽取-整理-推导
ch4-持久存储
Python 快捷键
cgitb--CGI跟踪模块(简化异常调试)
[转]NHibernate之旅(3):探索查询之NHibernate查询语言(HQL)
[转]NHibernate之旅(2):第一个NHibernate程序
[转]NHibernate之旅(1):开篇有益
TeamViewer 远程时出现:现在无法捕捉画面。这可能是由于恰的用户切换或远程桌面会话断开、最小化
Oracle自动备份
Copyright © 2011-2022 走看看