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文档的书签替换操作的更多相关文章

  1. C# 中使用Word文档对图像进行操作

    C# 中使用Word文档对图像进行操作 Download Files: ImageOperationsInWord.zip 简介 在这篇文章中我们可以学到在C#程序中使用一个Word文档对图像的各种操 ...

  2. 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 ...

  3. word文档常用的格式操作(分页、分节、每一节如何设置页码、以及生成目录)

    分页: 分节: 自动生成目录:

  4. C# Word文档中插入、提取图片,文字替换图片

    Download Files:ImageOperationsInWord.zip 简介 在这篇文章中我们可以学到在C#程序中使用一个Word文档对图像的各种操作.图像会比阅读文字更有吸引力,而且图像是 ...

  5. POI生成word文档完整案例及讲解

    一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...

  6. django admin 中实现word文档下载

    为了实现此功能,需要用到的知识点: (1)django  admin  自定义字段:   参考:http://www.cnblogs.com/wumingxiaoyao/p/6928297.html ...

  7. [办公应用]我的WORD文档表格操作不灵活 无法调整列宽

    最近同事的一个word文档中的表格操作非常不灵活,用鼠标直接调整列宽时总觉得很不灵活.她的操作系统为XP,office 为微软office 2003. 我首先检查了木马,检查了输入法等,结果都没有问题 ...

  8. 利用POI操作不同版本号word文档中的图片以及创建word文档

    我们都知道要想利用java对office操作最经常使用的技术就应该是POI了,在这里本人就不多说到底POI是什么和怎么用了. 先说本人遇到的问题,不同于利用POI去向word文档以及excel文档去写 ...

  9. Word文档数据被误删了怎么办,还能恢复吗

    很多时候由于时间紧张或者是思路不想被打断,我们在编辑Word时不能及时的手动保存,一旦遇到电脑意外断电的情况可能就会导致编辑好的Word文档内容丢失.或者是文档编辑好了之后,Word提示是否保存时,误 ...

随机推荐

  1. MySQL:基础—数据分组

    MySQL:基础-数据分组 1.为什么要分组: 比如一个表中有多条订单记录,如上图,每条记录对应着一个商品,现在我要查询 每个商品被订购的单数 准备出货?也就是找到每个商品被订购的数量. 如果只找一个 ...

  2. HTML5移动Web开发(十)——在浏览器中启动手机原生应用

    用户可以在浏览器中启动移动设备的原生应用程序,比如地图.电话.短信等,具体能够启动哪些应用程序,这取决于该移动设备上哪些原生应用是否允许从浏览器启动. 新建ch02r05.html <!doct ...

  3. [linux] is not in the sudoers file

    $su - $visudo append usrname ALL=(ALL) ALL save done ctrl+d

  4. Windows Azure HandBook (4) 分析Windows Azure如何处理Session

    <Windows Azure Platform 系列文章目录> 本文是对笔者之前的文章Windows Azure Cloud Service (13) 多个VM Instance场景下如何 ...

  5. 简单的使用ehcache

    之前一直感觉缓存是高上大的东西,没有心思去研究.做了之后发现,简单的使用还是很容易的.这里记录ehcache在jfinal中的简单使用. 1.ehcahe简介 EhCache 是一个纯Java的进程内 ...

  6. Elasticsearch聚合 之 Terms

    之前总结过metric聚合的内容,本篇来说一下bucket聚合的知识.Bucket可以理解为一个桶,他会遍历文档中的内容,凡是符合要求的就放入按照要求创建的桶中. 本篇着重讲解的terms聚合,它是按 ...

  7. Node魔法堂:NPM入了个门

    一.前言 NPM作为Node的模块管理和发布工具,作用与Ruby的gem.Python的pypl或setuptools.PHP的pear和.Net的Nuget一样.在当前前端工程化极速狂奔的年代,即使 ...

  8. python基础知识理解

    一.概述 看了一天的python基础语法,基本对python语法有了一个大概的了解(其实之前断断续续也看过python),学习网址:Python 基础教程.因为之前我学过C++,因此在学习python ...

  9. 博客已经转移到http://geekori.cn,本博客不再更新

    已经建立了独立博客:极客起源(http://geekori.cn),并将大部分博文已经转移到新的博客,本博客不再更新.

  10. Oracle导入导出命令

    //导出 exp techrpt_data/techrpt_data@orcl file=d:\_临时文件\techrpt_data.dmp owner=techrpt_data //导入 imp t ...