OperationExcel (C #)
Compile:Http://www.aspcn.comFlying knife
Original:Arungg
Source:Http://www.csharphelp.com/archives/archive241.html
I will not translate the above. What foreigners operate is Excel 2000 . All Excel OfProgramAll operations are from Excel Object Library Excel9.olb. This example is just a simple operation for this stuff. Some people tell me what they want to know. :) Excel Annoying Brothers refer to one " Minglu " Right :)
The first step is to use Tlbimp This tool will Excel9.0 Object Library File Excel8.olb Convert DLL In this way . Net Platform Assembly To use:
Tlbimp excel9.olb excel. dll
If you have thisExcel. dllNow we can useExcelThe various operation functions.
Next let's take a lookC #How to use these things.
1.Create a newExcelOfApplication:
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.ObtainWorkbooksSet:
Workbooks workbooks = exc. workbooks;
4.Add newWorkbook:
_ Workbook = workbooks. Add (xlwbatemplate. xlwbatworksheet, 0 );
5.ObtainWorksheetsSet:
_ 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 couldn't be started! ");
Return 0;
}
Exc. set_visible (0, true );
Workbooks workbooks = exc. workbooks;
_ Workbook = workbooks. Add (xlwbatemplate. xlwbatworksheet, 0 );
Sheets sheets = Workbook. worksheets;
_ 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");
}< br> const int ncells = 1;
object [] args1 = new object [1];
args1 [0] = ncells;
range1.gettype (). invokemember ("value", bindingflags. setproperty, null, range1, args1);
return 100;
}< BR >}
now let's take a look at how to use arrays, which is similar to setting cells. The only 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 [I] = I + 1;
}< br> object [] args2 = new object [1];
args2 [0] = array2;
range2.gettype (). invokemember ("value", bindingflags. setproperty, null, range2, args2);