First, you must reference a com reference: Microsoft Excel 11.0 Object Library.ProgramCodeAdd a reference to the namespace Microsoft. Office. InterOP. Excel. Then you can operate with Excel.
Basic Excel objects
Application: each application object represents an excel program. an instance of the EXCEL program is started when this object is created in the program.
Workbook: Each workbook object indicates an Excel worksheet. Multiple workbooks can be opened in the same EXCEL program.
Worksheet: Each worksheet object represents an excle worksheet. Multiple worksheets can be opened in the same Excel worksheet.
Range: each range object indicates an area in an Excel worksheet. the area can be a cell or a combination of multiple cells. The area can be continuous or discrete.
Simple code for Excel operations
Private void button#click (Object sender, eventargs e) {Microsoft. office. interOP. excel. application app1 = new Microsoft. office. interOP. excel. application (); app1.visible = true; workbook book1 = app1.workbooks. add (type. missing); wroksheet sheet1 = (worksheet) book1.sheets [1]; range rng1 = sheet1.get _ range ("A1", type. missing); RNG. value2 = "hello ";}
After clicking the button, you will find that a new Excel file is created. This Excel file contains a table that displays the text hello in the cell A1.
Workbook book1 = app1.workbooks. Add (type. Missing );
In addition to the add method, you can use the open method to open an existing table and operate it.
To obtain the region value, an array of the object type is returned:
Sheet1.get _ range ("B2", type. missing); // return the B2 cell sheet1.get _ range ("A1: C10", type. missing); // return the sheet1.get _ range ("A1", "C10) of the C10 area cell from the upper left corner to the lower right corner ); // The effect is the same as that of the previous row (cell A1 to cell C10 in the lower right corner in the upper left corner). sheet1.get _ range ("A: A", type. missing); // returns the entire a column sheet1.get _ range ("A: C", type. missing); // returns the sheet1.get _ range ("", type. missing); // returns the sheet1.get _ range ("A1: A5, C1: C5", type. missing); // returns two independent regions
In addition to getting the value of a region, you can obtain the value of a single cell:
Range rng1 = (range) sheet1.cells [1, 1]; // get range rng2 = (range) sheet1.cells [3, "C]; // obtain range rng3 = (range) sheet1.cells [2, type. missing]; // get the B1 Cell
In addition to the value2 attribute, the range object also has the value attribute. This attribute is used only when the date and currency formats are involved. Otherwise, the value2 attribute should be used to keep the program concise and consistent.
The range object also has a read-only attribute: Text. Although this attribute is of the object type, all value types are processed in text format. therefore, if you have determined the Data Type of the text when using this text attribute, you can use the parse method to convert the text to this data type. if not, you can use the tryparse method.
In addition, if the values of one or some cells in a range object are all numbers, and we do not want to process these values by number, we want to convert them into text or other formats. the numberformat attribute of range can be used.
There is also a special control called the spreedsheet Control for operating Excel tables. However, when I was doing something, I wanted to display the date format data correctly in this control, which caused a lot of problems, finally, the control is discarded. finally, we selected the more common datagridview -_-
More information: http://support.microsoft.com/kb/302084/en-us