2012-04-25 75 views
0
public string Name(string code) 
    { 
     MyExcelAppInstance.Volatile(true); 

     MsExcel.Application oApp = new MsExcel.Application(); 
     oApp.Visible = true; 
     oApp.UserControl = true; 
     Object oBooks = oApp.Workbooks; 
     System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US"); 
     oBooks.GetType().InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, oBooks, null, ci); 

     oApp.Visible = true; 
     oApp.UserControl = true; 
     System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture; 
     System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); 
     oApp.Workbooks.Add(); 
     System.Threading.Thread.CurrentThread.CurrentCulture = oldCI; 

     MsExcel.Range rng = MyExcelAppInstance.get_Range("A1:D4", Type.Missing); 
     //Get a 2D Array of values from the range in one shot: 
     object[,] myArray = (object[,])rng.get_Value(Type.Missing); 

     // Process 'myArray' however you want here. 
     // Note that the Array returned from Excel is base 1, not base 0. 
     // To be safe, use GetLowerBound() and GetUpperBound: 
     for (int row = myArray.GetLowerBound(0); row <= myArray.GetUpperBound(0); row++) 
     { 
      for (int column = myArray.GetLowerBound(1); column <= myArray.GetUpperBound(1); column++) 
      { 
       if (myArray[row, column] is double) 
       { 
        myArray[row, column] = (double)myArray[row, column] * 2; 
       } 
      } 
     } 

     // Pass back the results in one shot: 
     rng.set_Value(Type.Missing, myArray); // where gets an error 

     return code + code; 
    } 

我收到一個錯誤,「異常來自HRESULT:0x800A03EC」。我試圖設置區域設置,但沒有奏效。沒有set_value,它工作正常。任何想法,爲什麼這是行不通的?C#Excel UDF範圍寫回得到一個錯誤,異常來自HRESULT:0x800A03EC

+0

你試過了Value2嗎?你從哪裏調用這個函數?是保護表? – 2012-04-25 15:44:06

+0

我也嘗試了Value2,但沒有工作,工作表沒有得到保護 – icewall 2012-04-26 00:25:03

回答

0

我有一個類似的問題一次,錯誤竟然是由一個無效的參數(我以無效格式傳遞範圍字符串)引起的。確保當您致電set_Value時,範圍和所有數組內容是有效的。

相關問題