Manipulating Excel (C #)

Source: Internet
Author: User
Tags range
Excel from Http://www.pconline.com.cn/pcedu/empolder/gj/vc/10203/43433.html
Manipulating Excel (C #)
Source: Pconline
Responsible Editor: ZWG

[02-3-29 9:33] Author: Flying Knife. net/aspcn.com

Manipulating Excel (C #)

Compiling: http://www.aspcn.com Flying knife
Original: Arungg
Source: http://www.csharphelp.com/archives/archive241.html

I will not translate the front. The foreigner operates Excel 2000. All Excel program actions are derived from the object library Excel9.olb of Excel. This example is just a simple operation to understand this dongdong. There is a warning to be friends of the specific understanding: for our site every day for Excel annoying brothers, refers to a "Ming Road" bar:
The first step is to use the TlbImp tool to convert the Excel9.0 object library file Excel8.olb into a DLL, which can be used as a. NET Platform assembly:

TLBIMP Excel9.olb Excel.dll

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

1. Create a new Excel application:


Application exc = new application ();
if (exc = = null) {
Console.WriteLine ("Error:excel couldn ' t be started");
return 0;
}


2. Make this project visible:

Exc.set_visible (0, true);

3. Get Workbooks Collection:

Workbooks workbooks = exc. workbooks;

4. Join the new workbook:

_workbook workbook = workbooks. ADD (xlwbatemplate.xlwbatworksheet, 0);

5. Get Worksheets Collection:


_worksheet worksheet = (_worksheet) sheets.get_item (1);
if (worksheet = = null) {
Console.WriteLine ("ERROR in worksheet = null");
}
6. Set the variable to the cell:


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);


Routines:


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 couldn ' t 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 look at how to use an array, and he's somewhat similar to setting cells. The only need for change 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 results:



You need to understand the use of tlbimp this tool AH: This dongdong is very useful, you can transfer ordinary WIN32 program to. NET:

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.