使用Excel互操作你可以這樣做。我認爲函數名稱指向的是在你的Excel工作簿中定義的函數,否則你必須改變這個部分來使用反射(但在這種情況下,你需要一個接收器的方法,我沒有看到一個在問題中)。
object hmissing = System.Reflection.Missing.Value;
Xls.Application App = new Xls.ApplicationClass();
App.Visible = true;
Xls.Workbook wb = App.Workbooks.Open(@"c:\tmp\cartel1.xls", hmissing, hmissing, hmissing, hmissing, hmissing, hmissing,
hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing);
Xls.Worksheet ws = (Xls.Worksheet)wb.ActiveSheet;
Xls.Range rng = ws.UsedRange;
Xls.Range hdr = rng.Find("Function Name", hmissing, hmissing, hmissing, hmissing, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, hmissing, hmissing, hmissing);
string hdrAdd = hdr.get_Address(hmissing, hmissing, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, hmissing, hmissing);
string[] pcs = hdrAdd.Split('$');
string col = pcs[1];
int row;
int.TryParse(pcs[2], out row);
string methName;
row++;
while ((methName = App.get_Range(col + row.ToString(), hmissing).get_Value(hmissing) as String) != null)
{
App.Run(methName.Split('.')[1], hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing,
hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing,
hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing, hmissing,
hmissing, hmissing, hmissing);
row++;
}
不錯,我不知道這個庫 - 一定會在未來的項目中使用它。謝謝 – Kane
本週我也只發現這個圖書館:) – leppie