C # basic Excel operations

Source: Internet
Author: User

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

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.