1. Import Support class libraries for office and Excel
On the reference of the project, add a new reference and select Microsoft Excel ** Object Library and Microsoft Office ** Object Library
After adding the package, you can get the reference packages Microsoft. Office. Core and Microsoft. Office. InterOP. Excel.
2. Use the introduced package to the project.
using Microsoft.Office.Core;using Microsoft.Office.Interop.Excel;
3. Open the Excel application, that is, the EXCEL program.
Application exapp = new Microsoft. Office. InterOP. Excel. Application (); // EXCEL program object
4. Open the Excel Workbook, that is, the document to be operated in the EXCEL program, that is, the Excel file (because Excel is a program that processes the structure of multiple documents)
Workbooks workbooks = exapp. workbooks; // Excel Workbook set object _ workbook _ WKB = workbooks. open (this. expath, 0, false, 5, "", "", true, Microsoft. office. interOP. excel. xlplatform. xlwindows, "", true, false, 0, true, false, false); // an Excel worksheet object
5. Open the sheet table to be operated on in the Excel Workbook (that is, the Excel document ).
Sheets sheets = _ WKB. sheets; // an Excel sheet set object _ worksheet _ wsh = (_ worksheet) sheets. get_item (1); // an Excel sheet object
6. Get the cell text
String S = (range) _ wsh. cells [4, 2]). Text; // obtain the B2 Value
7. Write cell text
_ Wsh. cells [4, 2] = "B2 value"; // write B2 value _ WKB. Save (); // Save the Excel document
8. Exit the Excel application
exApp.Quit();
The following are complex operations (to be supplemented ):
C # basic Excel operations