码迷,mamicode.com
首页 > Windows程序 > 详细

C#关于word文档的书签替换操作

时间:2016-08-22 12:38:40      阅读:655      评论:0      收藏:0      [点我收藏+]

标签:

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文档的书签替换操作

标签:

原文地址:http://www.cnblogs.com/fkcxy/p/5794818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!