First use the .NET 2003 / SDK / V1.1 / BIN directory to execute Tlbimp Excel.exe under the command prompt in the command prompt. This will not be Excel is a different * .olb file different from XP or 2000, and it is because there is no Excel9.olb file in the version after 2000. We get an Excel.dll file after performing TLBIMP Excel.exe. 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 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
]; arggs1 [
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 an array, he is some similar to setting a cell. 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.getlength (
0
I
)
{Array2 [i] = i 1;}
Object [] ARGS2
=
New
Object [
1
]; argg2 [
0
]
=
Array2; Range2.gettype (). InvokeMember
"
Value
"
BindingFlags.SetProperty,
NULL
, Range2, Args2;
Everyone needs to know the use of TLBIMP tools :) This stuff is useful, you can transplant the ordinary Win32 program to. Net below :) If the format of the Excel is simple, it is the structure of the general table, then actually Excel The file is almost the same as the method of operating the Access database file. (Note that it is to pay attention to, 1, the program will use the first line record in the ExCle table as the column name;
Below, I will give you a post on how to connect and read the code of the Excel file:
DataSet DS
=
New
DataSet (); OLEDBDataAdapter Ad;
String
STRDBPATH
=
"
./code.xls
"
;
String
Strconn
=
"
Provider = Microsoft.jet.Oledb.4.0; data source =
"
Server.mappath (strDbpath)
"
Extended Properties = Excel 8.0;
"
OLEDBCONNECTION CONN
=
New
OLEDBCONNECTION (STRCONN); conn.open ();
String
strsql
=
"
Select * from [Stock Code $]
"
AD
=
New
OLEDBDataAdapter (strsql, conn); ad.fill (ds); dg1.datasource
=
DS.TABLES [
0
]. DEFAULTVIEW;
//
DG1 is a DataGrid control
DG1.DATABIND ();
//
Keep the records in the stock code in the excle to the DataGrid control
If you use it under ASP.NET, I have to remember
Add to ISTEM.WEB>
Otherwise it will appear "
Abnormal Details: System.unauthorizedAccessException: Deny Access.