C # Excel operations and Analysis

Source: Internet
Author: User

Today, I wrote a tool for ELE. Me. I want to dynamically read the IP address and port number on Excel to change the proxy address of IE. Since I haven't written any Excel operation for a long time, I can only check my previous projects, summary:
First, we need to reference the Excel of our COM interface
The Microsoft. Office. InterOP. Excel. Application Excel Object indicates the Excel application.ApplicationThe object exposes a large amount of information about running applications, options applied to the instance, and the objects of the current user opened in the instance.
Note:ApplicationThe enableevents attribute of the object is set to false. Setting this attribute to false will prevent EXCEL from triggering any events, including events of the Host Control.
Workbook object
Microsoft. Office. InterOP. Excel. WorkbookClass indicates a single workbook in an Excel application
Worksheet object
Microsoft. Office. InterOP. Excel. WorksheetAn object is a member of the worksheets set.Microsoft. Office. InterOP. Excel. WorksheetMany Attributes, methods, and eventsApplicationOrMicrosoft. Office. InterOP. Excel. WorkbookClass provides identical or similar members.

Excel provides the sheet setMicrosoft. Office. InterOP. Excel. WorkbookObject Attributes, but not in ExcelSheetClass. On the contrary,SheetsEach member of the set isMicrosoft. Office. InterOP. Excel. WorksheetOr a Microsoft. Office. InterOP. Excel. Chart object.
Range object

Microsoft. Office. InterOP. Excel. RangeObjects are the most common objects in Excel applications. It must be representedRangeAnd process the methods and attributes of the object.RangeAn object represents a cell, a row, a column, a selected range of cells that contain one or more cell blocks (can be continuous or discontinuous), or even a group of cells in multiple worksheets.

Write operation:
Microsoft. Office. InterOP. Excel. applicationclass myexcel = newmicrosoft. Office. InterOP. Excel. applicationclass ();
Myexcel. Visible = false; // whether the Excel file is visible
Myexcel. displayalerts = false; // some pop-up windows are blocked.
Microsoft. Office. InterOP. Excel. workbooks myworkbooks = myexcel. workbooks;
Microsoft. Office. InterOP. Excel. Workbook myworkbook = myworkbooks. Add (system. type. Missing );
Microsoft. Office. InterOP. Excel. worksheet myworksheet = (Microsoft. Office. InterOP. Excel. worksheet) myworkbook. worksheets [1]; // specify a sheet
Microsoft. Office. InterOP. Excel. Range myrange = myworksheet. get_range ("A1", "d1"); // You can dynamically set the range region for a specified region,
Finally, convert the data such as datatable into a two-dimensional array and assign the value to this area,
Object [,] mydata;
Myrange. value2 = mydata;
If you give a single value to excel, we can
Myworksheet. get_range ("A1", system. type. Missing). value2 = "aaaaaaaaa ";

Read operation:
Microsoft. Office. InterOP. Excel. Application Excel = new Microsoft. Office. InterOP. Excel. Application ();
Object nothing = system. type. missing;
Excel. application. workbooks. open (application. startuppath + "\ redex.xls", nothing ); // open a workbook
Microsoft. Office. InterOP. Excel. workbooks WBS = excel. workbooks
Microsoft. Office. InterOP. Excel. Workbook WB = WBS [1];
Microsoft. Office. InterOP. Excel. worksheet Ws = (Microsoft. Office. InterOP. Excel. worksheet) WB. worksheets ["sheet1"];
String sexa = ws. get_range ("A" + num, system. type. Missing). value2.tostring ();
String sexb = ws. get_range ("B" + num, system. type. Missing). value2.tostring ();
Excel Analysis
// Load macro
Microsoft. Office. InterOP. Excel. applicationclass myexcel;
Myexcel. addins ["analysis tool library-VBA function"]. Installed = false;
Myexcel. addins ["analysis tool library-VBA function"]. Installed = true;
Macro analysis allows you to view the name of a macro recorded in Excel.
Myworksheet is a work page
Myexcel. run ("pttestm", myworksheet. get_range ("E2", "E" + icount. tostring (), myworksheet. get_range ("h2", "H" + icount. tostring (), myworksheet. get_range ("m2", system. type. missing), false, 0.05, 0, system. type. missing, system. type. missing, system. type. missing,
System. type. Missing, system. type. Missing,
System. type. Missing, system. type. Missing,
System. type. Missing, system. type. Missing, system. type. Missing
, System. type. Missing );

 

C # Excel operations and Analysis

Related Article

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.