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);
}
}
}
}
查看全文
相关阅读:
SQL语句:Mac 下 处理myql 不能远程登录和本地登录问题
react中触发事件实现路由跳转
Sass代码重用----Sass继承
react实现点击选中的li高亮
React中通过状态控制元素显示隐藏的三种方法
React 页面加载后自动执行onClick事件
formdata实现图片上传
formdata实现图片上传
适用于Ant design的省市区联动JSON数据
react.js
原文地址:https://www.cnblogs.com/sunheyubo/p/995254.html
最新文章
为什么前端是最适合设计师学习的编程技能?
如何入门游戏开发?游戏开发需要掌握哪些技能?
分布式结构化存储系统HBase
分布式文件系统HDFS
1071 -Specified key was too long; max key length is 767 bytes
get和post方法的区别
第三方库安装后仍然import失败,提示ModuleNotFoundError:No module named'XXX'
python中requests里.text和.content方法的区别
在Python运行过程中遇到了如下错误: TypeError: ‘list’ object is not callable
数仓中的全量表、增量表、拉链表、流水表、快照表
热门文章
OLAP简介
SQL语言分类
Python入门学习笔记1:Python入门导学
CentOS7下RabbitMQ服务安装配置
mac 查询端口被哪个进程占用
ElasticSearch入门3: Spring Boot集成ElasticSearch
(转载)Centos下Elasticsearch安装详细教程
ElasticSearch入门3: 高级查询
(转载)elasticsearch 查询(match和term)
(转载)es进行聚合操作时提示Fielddata is disabled on text fields by default
Copyright © 2011-2022 走看看