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.