操作Excel(C#)

來源:互聯網
上載者:User

操作Excel(C#)

編譯:http://www.aspcn.com 飛刀
原著:ArunGG
來源:http://www.csharphelp.com/archives/archive241.html

  前面的我就不翻譯了。老外操作的是Excel 2000。所有Excel的程式操作都來源於Excel的物件程式庫Excel9.olb.本例也只是對這個東東做一個簡單的操作瞭解。有告誡待於朋友們的具體瞭解:)也算是為我們網站上天天為Excel煩的兄弟們,指一條“明路”吧:)
首先的一步就是使用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);


常式:


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);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.