最近在一个网逻站,看了几篇文章,觉得很不错,转贴过来^_^不过,暂未贴完,这不允许贴太多,那的东东也不少,还可以申请[email protected]的邮箱^_^,大家也可以直接去看:http://www.weblogic.com.cn首先的一步就是使用Tlbimp这个工具将Excel9.0的对象库文件Excel8.olb转换成为dll,这样才能做为.Net平台Assembly来使用:)操作如下:
TlbImp Excel9.olb 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);
(未完待续……)大家也可以直接去http://www.weblogic.com.cn看完,还有图解。
TlbImp Excel9.olb 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);
(未完待续……)大家也可以直接去http://www.weblogic.com.cn看完,还有图解。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货