首先將excel.exe copy 到 ..\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin目錄下
利用.net 中帶的工具在命令提示符下執行tlbimp excel.exe.這樣就不會因為你的Excel是xp或2000的不同要去找不同的*.olb文件,還有一點就是因為在2000以后的版本中沒有了excel9.olb這個文件了。
通過執行tlbimp excel.exe后我們會得到excel.dll文件。
只要有了這個Excel.dll,現在我們就能使用Excel的各種操作函數了。
下面就讓我們具體看看C#是如何使用這些東東吧。
1. 創建一個新Excel的Application:
Application exc = new Application();
if (exc == null) {
Console.WriteLine("ERROR: EXCEL couldn't be started");
return 0;
}
2. 讓這個工程可見:
exc.set_Visible(0, true);
3. 獲取WorkBooks集合:
Workbooks workbooks = exc.Workbooks;
4. 加入新的WorkBook:
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0);
5. 獲取WorkSheets集合:
_Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
if (worksheet == null) {
Console.WriteLine ("ERROR in worksheet == null");
}
6. 給單元格設置變量:
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);
例程:
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;
}
}
現在我們來看看如何使用數組,他有些類似于設置單元格。僅僅需要的改變只是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);
大家需要了解Tlbimp這個工具的使用啊:)這個東東很有用,可以將普通Win32程序移植到.Net下面來:)
如果操作的excel的格式很簡單,就是一般的表的結構,那么其實操作EXCEL文件跟操作ACCESS數據庫文件的方法幾乎一樣。
(需要注意的地方就是,1、程序會把EXCLE表中的第一行記錄作為列名;2、在使用EXCLE表的時候, 要在表名后面加上符號$)
下面,我給你帖一段如何連接和讀取EXCEL文件的代碼吧:
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 [股票代碼$]";


ad = new OleDbDataAdapter(strSQL, Conn);
ad.Fill(ds);

dg1.DataSource = ds.Tables[0].DefaultView; //dg1是一個DataGrid控件
dg1.DataBind(); //將EXCLE中股票代碼中的記錄棒定到DataGrid控件上


如果是在asp.net 下使用的話,要記得在 中添加
否則就會出現 “異常詳細信息: System.UnauthorizedAccessException: 拒絕訪問“。