C#关于word文档的书签替换操作
public void Get_Word(string gjbh)
{
try
{
DataSet ds = OperaterBase.GetDsBySql("select diffTableName from public_Data where gjbh='" + gjbh + "'");
string mode_bh = ds.Tables[0].Rows[0]["diffTableName"].ToString();
//模板word路径
string source = HttpContext.Current.Server.MapPath("../WorkData/Model_doc/") + mode_bh + ".doc";
//根据估价编号生成的Word路径
string destination = HttpContext.Current.Server.MapPath("../WorkData/Pgbg_Doc/") + gjbh + ".doc";
//拷贝内容
Doc_mbtowsPage_copy(source, destination);
Object oMissing = System.Reflection.Missing.Value;
WordApp = new Microsoft.Office.Interop.Word.Application();
WordApp.Visible = false;
object filename = HttpContext.Current.Server.MapPath("../WorkData/Pgbg_Doc/") + gjbh + ".doc";
WordDoc = WordApp.Documents.Open(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);
WordDoc.Activate();
object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
object BreakType = Microsoft.Office.Interop.Word.WdBreakType.wdSectionBreakNextPage;
object confirmConversion = false;
object link = false;
object attachment = false;
object NameBookMark = "";
System.Collections.IEnumerator enu = WordDoc.Bookmarks.GetEnumerator();
//加载数据
string dft_val_sql = sql;//从数据库得到要替换内容的sql语句
System.Data.DataTable dt1 = dataopr.data_get_with_datareader(dft_val_sql);
string b = "";
int n = 0;
//查找书签替换
while (enu.MoveNext())
{
str = ((Microsoft.Office.Interop.Word.Bookmark)enu.Current).Name;
b += "," + str;
n++;
Microsoft.Office.Interop.Word.Range r;
word_sq_name = str.Split('_')[0];
r = WordDoc.Bookmarks.get_Item(str).Range;//得到书签的位置
if (dt.Columns.Contains(word_sq_name) || word_sq_name == "c")
{
//区位图
switch (word_sq_name)
{
case "cjrq":
if (str == "cjrq_uc")
{//大写替换
r.Text = data_convert.numToCnum_transfer(data_convert.Y_M_D_transfer(dt.Rows[0][word_sq_name].ToString(), "1"));
WordDoc.Bookmarks.Add(str, r);
}
else
{
r.Text = data_convert.Y_M_D_transfer(dt.Rows[0][word_sq_name].ToString(), "0");
WordDoc.Bookmarks.Add(str, r);
}
break;
case "pgjzdx"://pgjzdx_uc_2
if (str.Split('_')[1] == "uc")
{//大写替换ConvertSum
r.Text = data_convert.ConvertSum(dt.Rows[0]["pgjzxx"].ToString());
WordDoc.Bookmarks.Add(str, r);
}
else
{
r.Text = dt.Rows[0]["pgjzxx"].ToString();
WordDoc.Bookmarks.Add(str, r);
}
break;
case "sczktz":
string jzsd1 = dt.Rows[0]["jzsd1"].ToString();
jzsd1 = jzsd1.Replace("-", "");//.Replace("月", "-").Replace("日", "-");
//jzsd=DateTime.ParseExact(dt.Rows[0]["jzsd"].ToString(), "yyyyMMdd", null).ToString();
jzsd1 = jzsd1.Substring(0, jzsd1.Length - 2);
string Insert_filename1 = HttpContext.Current.Server.MapPath("../WorkData/AdditionalSysMaintenance_doc/sczktz_" + jzsd1 + ".doc");
//E:\zhpg\WorkData\AdditionalSysMaintenance_doc\scbjfx_201607.doc
NameBookMark = str;
if (File.Exists(Insert_filename1))
{
WordApp.Selection.GoTo(ref Bookmark, ref oMissing, ref oMissing, ref NameBookMark);
WordApp.Selection.InsertFile(Insert_filename1, ref oMissing, ref confirmConversion, ref link, ref attachment);
//清除空行
//WordApp.Selection.TypeBackspace();
}
break;
case "scbjfx":
string jzsd = dt.Rows[0]["jzsd1"].ToString();
jzsd = jzsd.Replace("-", "");//.Replace("月", "-").Replace("日", "-");
//jzsd=DateTime.ParseExact(dt.Rows[0]["jzsd"].ToString(), "yyyyMMdd", null).ToString();
jzsd = jzsd.Substring(0, jzsd.Length - 2);
string Insert_filename = HttpContext.Current.Server.MapPath("../WorkData/AdditionalSysMaintenance_doc/scbjfx_" + jzsd + ".doc");//E:\zhpg\WorkData\AdditionalSysMaintenance_doc\scbjfx_201607.doc
NameBookMark = str;
if (File.Exists(Insert_filename))
{
WordApp.Selection.GoTo(ref Bookmark, ref oMissing, ref oMissing, ref NameBookMark);
WordApp.Selection.InsertFile(Insert_filename, ref oMissing, ref confirmConversion, ref link, ref attachment);
//清除空行
//WordApp.Selection.TypeBackspace();
}
break;
case "szlc":
if (str.Split('_').Length > 1)
{
if (str.Split('_')[1] == "uc")
{
r.Text = data_convert.NumToCnum(dt.Rows[0][word_sq_name].ToString());
WordDoc.Bookmarks.Add(str, r);
}
else
{
r.Text = dt.Rows[0][word_sq_name].ToString();
WordDoc.Bookmarks.Add(str, r);
}
}
else
{
r.Text = dt.Rows[0][word_sq_name].ToString();
WordDoc.Bookmarks.Add(str, r);
}
break;
case "zlc":
if (str.Split('_').Length > 1)
{
if (str.Split('_')[1] == "uc")
{
r.Text = data_convert.NumToCnum(dt.Rows[0][word_sq_name].ToString());
WordDoc.Bookmarks.Add(str, r);
}
else
{
r.Text = dt.Rows[0][word_sq_name].ToString();
WordDoc.Bookmarks.Add(str, r);
}
}
else
{
r.Text = dt.Rows[0][word_sq_name].ToString();
WordDoc.Bookmarks.Add(str, r);
}
break;
case "zzrq":
r.Text = data_convert.Y_M_D_transfer(dt.Rows[0][word_sq_name].ToString(), "0");
WordDoc.Bookmarks.Add(str, r);
break;
//case "gjzyrq"://没有
// {
// r.Text = data_convert.Y_M_D_transfer(dt.Rows[0][word_sq_name].ToString());
// WordDoc.Bookmarks.Add(str, r);
// break;
// }
case "startTime":
r.Text = data_convert.Y_M_D_transfer(dt.Rows[0][word_sq_name].ToString(), "0");
WordDoc.Bookmarks.Add(str, r);
break;
case "endTime":
r.Text = data_convert.Y_M_D_transfer(dt.Rows[0][word_sq_name].ToString(), "0");
WordDoc.Bookmarks.Add(str, r);
break;
case "sdkcrq":
r.Text = data_convert.Y_M_D_transfer(dt.Rows[0][word_sq_name].ToString(), "0");
WordDoc.Bookmarks.Add(str, r);
break;
case "fwcb":
if (dt.Rows[0][word_sq_name].ToString() == "")
{
WordDoc.Bookmarks.get_Item(str).Range.Rows.Delete();
WordDoc.Bookmarks.Add(str, r);
}
else
{
r.Text = dt.Rows[0][word_sq_name].ToString();
WordDoc.Bookmarks.Add(str, r);
}
break;
case "fwxz":
if (dt.Rows[0][word_sq_name].ToString() == "" && dt.Rows[0]["gyqk"].ToString() == "")
{
WordDoc.Bookmarks.get_Item(str).Range.Rows.Delete();
WordDoc.Bookmarks.Add(str, r);
}
else
{
r.Text = dt.Rows[0][word_sq_name].ToString();
WordDoc.Bookmarks.Add(str, r);
}
break;
case "fwsyqname":
string qyqk = dt.Rows[0]["gyqk"].ToString();
string fwxz = dt.Rows[0]["fwxz"].ToString();
string fwcb = dt.Rows[0]["fwcb"].ToString();
if (fwcb == "")
{
r.Text = "房屋所有权人";
}
else
{
r.Text = "房屋共有权人";
}
WordDoc.Bookmarks.Add(str, r);
break;
case "fwsyqzhame":
string qyqk1 = dt.Rows[0]["gyqk"].ToString();
string fwxz1 = dt.Rows[0]["fwxz"].ToString();
string fwcb1 = dt.Rows[0]["fwcb"].ToString();
if (fwcb1 == "")
{
r.Text = "房屋所有权证号";
}
else
{
r.Text = "房屋共有权证号";
}
WordDoc.Bookmarks.Add(str, r);
break;
case "qwt":
if (File.Exists(HttpContext.Current.Server.MapPath("../WorkData/res_qwt/" + gjbh + ".jpg")))//+ dt.Rows[0][str].ToString()
{
WordDoc.Bookmarks.get_Item(str).Select();
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
//在书签的位置添加图片
InlineShape inlineShape = WordApp.Selection.InlineShapes.AddPicture(HttpContext.Current.Server.MapPath("../WorkData/res_qwt/" + gjbh + ".jpg"), true, true, ref oMissing);
WordDoc.Bookmarks.Add(str, inlineShape.Range);
}
else
{//区位图不存在
r = WordDoc.Bookmarks.get_Item(str).Range;
r.Text = "";
WordDoc.Bookmarks.Add(str, r);
}
enu.MoveNext();
}
create_directory();
WordDoc.Save();
}
finally {
WordDoc.Close();
WordApp.Quit();
}
}
public void Doc_mbtowsPage_copy(string source, string destination)
{
System.IO.FileInfo file_s = new System.IO.FileInfo(source);
System.IO.FileInfo file_d = new System.IO.FileInfo(destination);
file_s.CopyTo(destination, true);
}
C#关于word文档的书签替换操作的更多相关文章
- C# 中使用Word文档对图像进行操作
C# 中使用Word文档对图像进行操作 Download Files: ImageOperationsInWord.zip 简介 在这篇文章中我们可以学到在C#程序中使用一个Word文档对图像的各种操 ...
- java和javascript获取word文档的书签位置对比
1.javascript:把IE浏览器的activex都打开,使用如下网页,可以看到书签顺序和位置: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- word文档常用的格式操作(分页、分节、每一节如何设置页码、以及生成目录)
分页: 分节: 自动生成目录:
- C# Word文档中插入、提取图片,文字替换图片
Download Files:ImageOperationsInWord.zip 简介 在这篇文章中我们可以学到在C#程序中使用一个Word文档对图像的各种操作.图像会比阅读文字更有吸引力,而且图像是 ...
- POI生成word文档完整案例及讲解
一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...
- django admin 中实现word文档下载
为了实现此功能,需要用到的知识点: (1)django admin 自定义字段: 参考:http://www.cnblogs.com/wumingxiaoyao/p/6928297.html ...
- [办公应用]我的WORD文档表格操作不灵活 无法调整列宽
最近同事的一个word文档中的表格操作非常不灵活,用鼠标直接调整列宽时总觉得很不灵活.她的操作系统为XP,office 为微软office 2003. 我首先检查了木马,检查了输入法等,结果都没有问题 ...
- 利用POI操作不同版本号word文档中的图片以及创建word文档
我们都知道要想利用java对office操作最经常使用的技术就应该是POI了,在这里本人就不多说到底POI是什么和怎么用了. 先说本人遇到的问题,不同于利用POI去向word文档以及excel文档去写 ...
- Word文档数据被误删了怎么办,还能恢复吗
很多时候由于时间紧张或者是思路不想被打断,我们在编辑Word时不能及时的手动保存,一旦遇到电脑意外断电的情况可能就会导致编辑好的Word文档内容丢失.或者是文档编辑好了之后,Word提示是否保存时,误 ...
随机推荐
- Kruskal算法(一)之 C语言详解
本章介绍克鲁斯卡尔算法.和以往一样,本文会先对克鲁斯卡尔算法的理论论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 最小生成树 2. 克鲁斯卡尔算法介绍 3 ...
- MFC 字符串类CString 源代码
原文:http://blog.csdn.net/benny5609/article/details/1926088 CString使用的是引用技术,可以共享数据(这个大家都知道),另外空的CStirn ...
- go环境import cycle not allowed问题处理
1.前言 今天在搭建Go语言环境,从https://golang.org/dl/上下载一个Go语言环境.环境变量配置刚开始如下配置: GOROOT=/home/go/bin go执行文件目录 ...
- html/css基础篇——关于浏览器window、document、html、body高度的探究
首先说明本人所理解的这几个元素的计算 window高度应当是文档所在窗口的可视高度(没有包括浏览器的滚动条),计算方法document.documentElement.clientHeight doc ...
- Windows 8.1 系统ISO镜像下载或自Win8应用商店升级方法
1)下载 -- 面向全体用户的Win8.1预览版ISO镜像下载(28日开放): 安装时请输入微软官方提供给大家的产品密钥:NTTX3-RV7VB-T7X7F-WQYYY-9Y92F. 64位简体中文版 ...
- HTTPS协议说明
HTTPS协议说明 基本现在最安全的网络连接就是使用https了,http协议有几个不安全的地方: 传输信息是明文的. -- http的传输信息是明文的,基本网络劫持下就束手就擒了. 不能防止篡改. ...
- 基于HTML5的WebGL结合Box2DJS物理引擎应用
上篇我们基于HT for Web呈现了A* Search Algorithm的3D寻路效果,这篇我们将采用HT for Web 3D来呈现Box2DJS物理引擎的碰撞效果,同上篇其实Box2DJS只是 ...
- 两种读写配置文件的方案(app.config与web.config通用)
第一种方法:采用MS现有的ConfigurationManager来进行读写 using System.Configuration; namespace Zwj.TEMS.Common { publi ...
- JAVA - HashMap,TreeMap迭代
1.使用for_each循环迭代 public class TestUnit { public static void main(String[] args) { HashMap hashMap=ne ...
- 显示XML文档时排序数据
先看XML文档: 也可拷贝下面代码另存为XMl文档: <stepList> <steps> <step> <order>1</order> ...