2012-04-25 15 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を試しましたか?どこからこの関数を呼び出していますか?シートは保護されていますか? –

+0

私もValue2を試しましたが、うまくいかず、シートが保護されていません – icewall

答えて

0

私は似たような問題が1回起こり、無効な引数(無効な形式で範囲文字列を渡していた)によってエラーが発生することが判明しました。 set_Valueを呼び出すときに、範囲とすべての配列のconentsが有効であることを確認してください。

関連する問題