From http://www.pconline.com.cn/pcedu/empolder/gj/vc/10203/43433.html
Operation Excel (C #)
Source: PConline
Editor in charge: ZWG
[02-3-29 9:33] Author: Flying .Net / aspcn.com
Operation Excel (C #)
Compilation: http://www.aspcn.com
Original: arungg
Source: http://www.csharphelp.com/archives/archive241.html
I didn't translate it in front. Foreigners operate in Excel 2000. All Excel's program operations are derived from Excel's object library Excel9.olb. This example is just a simple operation of this stuff. There is a specific understanding of your friends :) It is also a brother who is annoying for Excel every day, refers to a "bright road" :)
The first step is to use the TLBIMP tool to convert the Excel9.0's object library file Excel8.olb into a DLL, so that you can use the .NET platform assembly :) The operation is as follows:
TLBIMP Excel9.olb Excel.dll
As long as there is this Excel.dll, now we can use Excel's various operational functions.
Let's take a look at how C # is how to use these stuff.
1. Create a new Excel Application:
Application EXC = New Application ();
IF (EXC == NULL) {
Console.writeline ("Error: Excel Couldn't Be Started);
Return 0;
}
2. Let this project visible:
Exc.set_visible (0, TRUE);
3. Get the Workbooks collection:
Workbooks Workbooks = Exc.Workbooks;
4. Add new Workbook:
_Workbook workbook = workbooks.add (xlwbateMplate.xlwbatworksheet, 0);
5. Get the Worksheets collection:
_Worksheet Worksheet = (_Worksheet) Sheets.get_Item (1);
IF (Worksheet == Null) {
Console.writeline ("Error In Worksheet == Null);
}
6. Set variables to 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 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 take a look at how to use arrays, he is some similar to setting cells. Just only need to change just 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 [i] = i 1; } Object [] args2 = new object [1]; ARGS2 [0] = array2; Range2.gettype (). InvokeMember ("Value", BindingFlags.SetProperty, Null, Range2, Args2; Output results: Everyone needs to understand the use of TLBIMP tools :) This stuff is useful, you can transplant the ordinary Win32 program to. Net below :)