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;
}
_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;
}
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:
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.