C # Call the WORD processing instance Code (including excel ),

Source: Internet
Author: User

C # Call the WORD processing instance Code (including excel ),

Recently, the program life (http://www.manong123.com) a friend asked me to help him to do a small function, in fact, is to save the content in the Word documents to the database, can achieve the search and convert the EXCEL function, the demand is very simple, I couldn't think of it for a week. I will describe the requirements in detail:

Provide a template for a WORD document. Most of the documents in this WORD document are text, and an EXCEL table is inserted. The content of the WORD is as follows:

Confirmation of real estate value Certification

 

No.: (pay 2009) Price Confirmation No.

Deng conscription:

According to your Commission, our company will dispatch professional evaluators to set the number of the Real Estate ("House Ownership Certificate") in cuiyuan, DaXiang district to 0013210, and Deng wenbing, the house owner, has a total of 6/8 floors, site surveys were conducted.

The structure grade of the house to be evaluated is brick-and-mortar. The construction period is the end of 1990s, and the new degree is 9.

The following table lists the housing values confirmed:

Here is an EXCEL table.

 

Certification purpose: Transaction Tax

 

Note:

Shaoyang Real Estate Property Right Supervision and Administration Department price management

On-site evaluation: Huang Shengzhong

Approval:

February 10, 2009

It is to store the relevant content in this file into the database, and at the same time to implement the query, for example, according to the principal query, at the same time to the query results can be transferred to EXCEL.

This is the function. First, pick out the technical points used in it,

It is not difficult to read the content in WORD;

Microsoft. office. interop. word. applicationClass wordApp = new Microsoft. office. interop. word. applicationClass (); object file = nam; object nullobj = System. reflection. missing. value; try {Microsoft. office. interop. word. document doc = wordApp. documents. open (ref file, ref nullobj, ref upload, ref nullobj, ref nullobj, ref nullobj, ref nullobj); // doc. activeWindow. selection. wholeStory (); // doc. activeWindow. selection. copy (); // IDataObject data = Clipboard. getDataObject (); fileContent = doc. content. text; // read all the Text in WORD here}

It is difficult to read the data in EXCEL in the WORD file. I tried many methods and did not find the relevant information. Finally, I saw the VB code on a foreign forum, then, you can use it;

Foreach (Microsoft. office. interop. word. inlineShape ish in doc. inlineShapes) {if (ish. type = Microsoft. office. interop. word. wdInlineShapeType. wdInlineShapeEmbeddedOLEObject) {if (ish. OLEFormat. progID = "Excel. sheet.8 ") {// ish. OLEFormat. doVerb (ref nullobj); ish. OLEFormat. activate (); Microsoft. office. interop. excel. workbook objEXl = (Microsoft. office. interop. excel. workbook) ish. OLEFormat. object; bui LdArea = (objEXl. worksheets [1] as Microsoft. office. interop. excel. worksheet ). cells [2, 2] as Microsoft. office. interop. excel. range ). text. toString (); if (buildArea! = "") {BuildUnitPrice = (objEXl. worksheets [1] as Microsoft. office. interop. excel. worksheet ). cells [2, 3] as Microsoft. office. interop. excel. range ). text. toString (); buildCountPrice = (objEXl. worksheets [1] as Microsoft. office. interop. excel. worksheet ). cells [2, 4] as Microsoft. office. interop. excel. range ). text. toString (); userKind = "Residential";} else {buildArea = (objEXl. worksheets [1] as Microsoft. office. interop. excel. worksheet ). cells [3, 2] as Microsoft. office. interop. excel. range ). text. toString (); buildUnitPrice = (objEXl. worksheets [1] as Microsoft. office. interop. excel. worksheet ). cells [3, 3] as Microsoft. office. interop. excel. range ). text. toString (); buildCountPrice = (objEXl. worksheets [1] as Microsoft. office. interop. excel. worksheet ). cells [3, 4] as Microsoft. office. interop. excel. range ). text. toString (); userKind = "non-residential";} objEXl. application. quit ();}}}

For the application of the three regular expressions, I want to obtain the principal such as Deng conscription. I cannot use SUBSTRING to retrieve data. This is too low;

Regex reg1 = new Regex ("[\ u4E00-\ u9FFF] +", RegexOptions. ignoreCase); userName = reg1.Match (doc. paragraphs [4]. range. text ). value. trim (); // principal Regex reg2 = new Regex ("evaluators on real estate located in ([\ S | \ s] +)", RegexOptions. ignoreCase); HouseAddr = reg2.Match (fileContent ). groups [1]. value. trim (); // house address doc. paragraphs [5]. range. text Regex reg3 = new Regex ("No. ([\ S | \ s] +), house owner", RegexOptions. ignoreCase); propertyNo = reg3.Match (fileContent ). groups [1]. value. trim (); // The Real Estate ID doc. paragraphs [5]. range. text Regex reg4 = new Regex ("level of house ownership ([\ S | \ s] +)", RegexOptions. ignoreCase); propertyUser = reg4.Match (fileContent ). groups [1]. value. trim (); // house owner doc. paragraphs [5]. range. text Regex reg5 = new Regex ("layers/total layers ([\ S | \ s] +), on-site survey and evaluation", RegexOptions. ignoreCase); buildCount = reg5.Match (fileContent ). groups [1]. value. trim (); // level/total number of layers doc. paragraphs [5]. range. text Regex reg6 = new Regex ("build age: ([\ S | \ s] +), new degree:", RegexOptions. ignoreCase); buildYear = reg6.Match (fileContent ). groups [1]. value. trim (); // The date of creation doc. paragraphs [6]. range. text Regex reg7 = new Regex ("on-site evaluation: ([\ S | \ s] +) Review", RegexOptions. ignoreCase); evaluateUser = reg7.Match (fileContent ). groups [1]. value. trim (); // field evaluation doc. paragraphs [13]. range. text Regex reg8 = new Regex ("[\ d | \ s] + year [\ d | \ s] + month [\ d | \ s] + day ", regexOptions. ignoreCase); evaluateDate = reg8.Match (fileContent ). value. trim (); // doc. paragraphs [15]. range. text. trim () Time

Four to EXCEL, many people on the internet use the datagrid to directly convert to EXCEL, but friends of the program life say that this can only flip the first page of data, not suitable for the program he wants;

DataTable dt = GetAllData (); StringWriter sw = new StringWriter (); sw. writeLine ("No. \ t entrusting party \ t Property Authority \ t ownership certificate No. \ t housing construction area \ t (square meter) \ t build years \ t layers/layers \ t use nature \ t evaluation unit price (RMB/m2) \ t Evaluation total value (RMB) \ t field evaluators \ t evaluation date \ t remarks "); if (dt! = Null) {foreach (DataRow dr in dt. rows) {sw. writeLine (dr ["id"] + "\ t" + dr ["userName"] + "\ t" + dr ["propertyUser"] + "\ t" + dr ["propertyNo"] + "\ t" + dr ["HouseAddr"] + "\ t" + dr ["buildArea"] + "\ t" + dr ["buildYear"] + "\ t" + dr ["buildCount"] + "\ t" + dr ["userKind"] + "\ t" + dr ["buildUnitPrice"] + "\ t ""+ dr [" buildCountPrice "] +" \ t "+ dr [" evaluateUser "] +" \ t "+ dr [" evaluateDate "] +" \ t "+ "" );}} sw. close (); Response. addHeader ("content-disposition", "attachment; filename=MyExcelFile.xls"); Response. contentType = "application/excel"; Response. contentEncoding = System. text. encoding. getEncoding ("GB2312"); Response. write (sw); Response. end ();

5. KILL process: When I view data in EXCEL in WORD, I cannot close EXCEL. I need to use a program to close it;

Public void KillProcess (string processName) {System. diagnostics. process myproc = new System. diagnostics. process (); // obtain all opened processes try {foreach (Process thisproc in Process. getProcessesByName (processName) {if (! Thisproc. CloseMainWindow () {if (thisproc! = Null) thisproc. Kill () ;}} catch (Exception Exc) {throw Exc; // msg. Text + = "Kill" + processName + "failed! ";}}

Let's take a look at the program life. It's a good article.


C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

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.