C # bookmark Substitution action for Word documents

Source: Internet
Author: User

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 ();
Template Word Path
String Source = HttpContext.Current.Server.MapPath (".. /workdata/model_doc/") + Mode_bh +". Doc ";

Word path generated based on the valuation number
String destination = HttpContext.Current.Server.MapPath (".. /workdata/pgbg_doc/") + GJBH +". Doc ";

Copy content

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 ();
Loading data

string dft_val_sql = sql;//SQL statement to replace content from the database

System.Data.DataTable dt1 = Dataopr.data_get_with_datareader (Dft_val_sql);
String b = "";
int n = 0;

//Find bookmark substitution
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;//gets the location of the bookmark
if (dt. Columns.contains (word_sq_name) | | Word_sq_name = = "C")
{
//Location Map
Switch (word_sq_name)
{
Case "CJRQ":
if (str = = "Cjrq_uc")
{//uppercase Replace Replace
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")
{//Uppercase replace 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 ("Month", "-"). Replace ("Day", "-");
//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);
Clear empty lines
WordApp.Selection.TypeBackspace ();
}

Break
Case "SCBJFX":
String jzsd = dt. rows[0]["JZSD1"]. ToString ();
JZSD = jzsd. Replace ("-", "");//. Replace ("Month", "-"). Replace ("Day", "-");
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 Name BookMark);
WordApp.Selection.InsertFile (insert_filename, ref omissing, ref confirmconversion, ref link, ref attachment);
//Clear blank line
//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"://No
// {
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 = "house owner";
}
Else
{
R.text = "house ownership person";
}
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 = "House ownership certificate number";
}
Else
{
R.text = "Housing co-ownership certificate number";
}
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;
Add a picture to a bookmark's location
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
{//Location map does not exist
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 # bookmark substitution actions for Word documents

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.