Excel Secondary Development Series (1): Excel compiling model

Source: Internet
Author: User

ExcelProgramming Model Description

Reference Xu wenbing's blog: http://www.cnblogs.com/macroxu-1982/archive/2006/11/21/567305.html

Here, we roughly describe the hierarchical relationships of objects commonly used in Excel programming.

Excel Application represents the entire Microsoft Excel Application,

WorkBook stands for Microsoft Excel workbooks

Range indicates a cell, a row, a column, a selected area (this area can contain one or more consecutive cells), or a three-dimensional area.

Areas A Collection of subareas or continuous cell blocks in the selected area.

Borders indicates the border of the object.

Characters represents Characters in objects that contain text. AvailableCharactersModifies any character sequence contained in a complete text string.

Font contains the Font attributes of the object (such as the Font name, Font size, and Font color ).

ListRow indicates a row in the list object.

Errors indicates that the workbook in the region is incorrect.

 

Excel process processing in a program
Mr Xu uses the time push algorithm in the Excel process. Although it is a method, it is not a good method, this is because when the customer opens an Excel file within this time period, the application is closed while the user is closed. This is what the user does not want to see. Of course, running with multiple users is also problematic, as Xu said. As we know, it is a headache to release its resources by calling COM in Excel programming. In addition, it is difficult to comment on the debugging program, sometimes the line number does not match the current cursor, causing visual errors to the adjustment. Tuning also requires certain skills, so I will talk about it later. Back to the resource release question: I have also read a lot of information. There are generally the following solutions:

(1). Call the existing methods of the system to release the object before exiting. For example:

Excel. Application app = new Excel. Application (); // open one Excel application
.............

App. Quit (); // quit the Excel application and release resource

Generally, resources are released when the Quit () method is called.
(2). To release the resources, you can recycle the garbage again at RunTime after the running program exits. You can use the following method:

 

ReleaseCOM
Private void ReleaseCOM (object pObj)
{
Try {
System. Runtime. InteropServices. Marshal. ReleaseComObject (pObj );
}
Catch {
Throw new Exception ("Release resource Error! ");
}
Finally {pObj = null ;}
}

Private void Operate (string pFileName)
{
Microsoft. Office. Interop. Excel. Application app = new Excel. Application (); // open an Excel Application
If (app = null)
{
Return;
}
Workbooks wbs = app. Workbooks;
_ Workbook wb = wbs. Add (pFileName); // open an existing Workbook
Sheets shs = wb. Sheets;
_ Worksheet sh = (_ Worksheet) shs. get_Item (1); // select the first Sheet
If (sh = null)
{
Return;
}
Range r = sh. get_Range ("B1", Missing. Value );
If (System. Convert. ToString (r. Value2). Trim (). Equals ("my "))
{
// Do Something.
}
// Invoke ReleaseCOM function to release resource
ReleaseCOM (sh );
ReleaseCOM (shs );
ReleaseCOM (wb );
ReleaseCOM (wbs );
App. Quit ();
ReleaseCOM (app );
}

After testing, there is usually no problem. Pay attention to the release order.

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.