Operate Excel (C #)

Source: Internet
Author: User

I will not translate the above. Excel 2000 is used by foreigners. All Excel program operations come from Excel's object library Excel9.olb. This example only provides a simple understanding of this stuff. I have warned my friends about it. :) it's also an annoyance for our site. Let's refer to a "Bright Path :)
The first step is to use the Tlbimp tool to convert Excel9.0's object library file Excel8.olb to a dll so that it can be used as a. Net platform Assembly. The operations are as follows:

TlbImp Excel9.olb Excel. dll

With this Excel. dll, we can now use various Excel operation functions.
Next let's take a look at how C # uses these things.

1. Create a New Excel Application:

Application exc = new Application ();
If (exc = null ){
Console. WriteLine ("ERROR: EXCEL couldnt be started ");
Return 0;
}

2. Make this project visible:

Exc. set_Visible (0, true );

3. Get the WorkBooks set:

Workbooks workbooks = exc. Workbooks;

4. Add a new WorkBook:

_ Workbook workbook = workbooks. Add (XlWBATemplate. xlWBATWorksheet, 0 );

5. Get the WorkSheets set:

_ Worksheet worksheet = (_ Worksheet) sheets. get_Item (1 );
If (worksheet = null ){
Console. WriteLine ("ERROR in worksheet = null ");
}

6. Set variables for cells:

Range range1 = worksheet. get_Range ("C1", Missing. Value );
If (range1 = null ){
Console. WriteLine ("ERROR: range = null ");
}
Const int nCells = 1;
Object [] args1 = new Object [1];
Args1 [0] = nCells;
Range1.GetType (). InvokeMember ("Value", BindingFlags. SetProperty, null, range1, args1 );

Routine:

Using System;
Using System. Reflection;
Using System. Runtime. InteropServices;
Using Excel;

Class Excel {
Public static int Main (){
Application exc = new Application ();
If (exc = null ){
Console. WriteLine ("ERROR: EXCEL couldnt be started! ");
Return 0;
}

Exc. set_Visible (0, true );
Workbooks workbooks = exc. Workbooks;
_ Workbook workbook = workbooks. Add (XlWBATemplate. xlWBATWorksheet, 0 );
Sheets sheets = workbook. Worksheets;

_ Worksheet worksheet = (_ Worksheet) sheets. get_Item (1 );
If (worksheet = null ){
Console. WriteLine ("ERROR: worksheet = null ");
}

Range range1 = worksheet. get_Range ("C1", Missing. Value );
If (range1 = null ){
Console. WriteLine ("ERROR: range = null ");
}
Const int nCells = 1;
Object [] args1 = new Object [1];
Args1 [0] = nCells;
Range1.GetType (). InvokeMember ("Value", BindingFlags. SetProperty, null, range1, args1 );
Return 100;
}
}

Now let's take a look at how to use arrays, which are similar to setting cells. The only change required is args2 [0] = array2;
Const int nCell = 5;
Range range2 = worksheet. get_Range ("A1", "E1 ");
Int [] array2 = new int [nCell];
For (int I = 0; I <array2.GetLength (0); I ++ ){
Array2 [I] = I + 1;
}
Object [] args2 = new Object [1];
Args2 [0] = array2;
Range2.GetType (). InvokeMember ("Value", BindingFlags. SetProperty, null, range2, args2 );

Output result:

You need to know how to use the Tlbimp tool. :) this tool is very useful. You can port a common Win32 program to. Net :)

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.