我有下面的代碼在我的Excel插件DNA如何使用Excel Dna設置單元格的值?
在我的AutoOpen方法我把下面的代碼:
ExcelIntegration.RegisterUnhandledExceptionHandler(ex => ex.ToString());
我有以下功能會從我的Excel工作表中調用。
[ExcelFunction(Category = "Foo", Description = "Sets value of cell")]
public static Foo(String idx)
{
Excel.Application app = (Excel.Application)ExcelDnaUtil.Application;
Excel.Workbook wb = app.Workbooks[1];
Excel.Worksheet ws = GetSheet("Main");
// This gives us the row
Excel.Name idxRange = wb.Names.Item("COL_Main_Index");
var row = (int)app.WorksheetFunction.Match(idx, idxRange.RefersToRange, 0);
// Get the Column
Excel.Name buyOrderRange = wb.Names.Item("COL_Main_BuyOrder");
var col = (int)buyOrderRange.RefersToRange.Cells.Column;
// create the range and update it
Excel.Range r = (Excel.Range)ws.Cells[row, col];
r.Value ="Foo";
}
問題是我實際上無法設置任何單元格值。當我調用該方法時,會在最後一行中導致錯誤。
我的錯誤處理程序給我的followign錯誤:
{System.Runtime.InteropServices.COMException (0x800A03EC)
我也試圖設置單元格的值,像這樣:
r = (Excel.Range)ws.Cells[12, 22];
const int nCells = 1;
Object[] args1 = new Object[1];
args1[0] = nCells;
r.GetType().InvokeMember("Value2", BindingFlags.SetProperty, null, r, args1);
具有相同的結果。
任何人都可以指出我可能在這裏做錯了嗎?
和往常一樣Govert,你是對的。感謝您的確認。 – chollida 2013-02-19 15:41:17
其中一個解決方法可以在這裏找到: http://excel-dna.net/2011/01/30/resizing-excel-udf-result-arrays/ – Kukks 2013-07-09 08:17:48