带有C#的新excel公式/函数:无法隐式转换类型“;布尔”;至“;系统功能<;对象>”;
我们用C#创建了一个新的Excel公式/函数,部分代码如下:带有C#的新excel公式/函数:无法隐式转换类型“;布尔”;至“;系统功能<;对象>”;,c#,excel,function,formula,C#,Excel,Function,Formula,我们用C#创建了一个新的Excel公式/函数,部分代码如下: using ExcelDna.Integration; using Excel = Microsoft.Office.Interop.Excel; namespace Test_Atune { public class Dataset { public static string baseurl = Constant.URL_ROOT_COMMON; public static stri
using ExcelDna.Integration;
using Excel = Microsoft.Office.Interop.Excel;
namespace Test_Atune
{
public class Dataset
{
public static string baseurl = Constant.URL_ROOT_COMMON;
public static string headval = Constant.HEADVAL_COMMON;
public static Request request = new Request();
[ExcelFunction(Category = "test", IsMacroType = true, Description = "test dataset")]
public static object TEST_DATASET(
[ExcelArgument(Description = "test_code")] string test_1,
[ExcelArgument(Description = "YYYYMMDD")] string test_2,
[ExcelArgument(Description = "YYYYMMDD")] string test_3
)
{
string parstr = @"/test_dataset/?" +
@"test_1=" + test_1 +
@"&test_2=" + test_2 +
@"&test_3=" + test_3;
ExcelReference caller = (ExcelReference)XlCall.Excel(XlCall.xlfCaller);
Excel.Range rg = caller.ToPiaRange();
return ExcelAsyncUtil.Run("TEST_DATASET",
new object[] { parstr },
() => request.GetDatasetFromUrl(Constant.URL_ROOT_COMMON, parstr, headval, rg));
}
}
}
该函数工作正常,但当我们单击非空白区域并同时单击Excel函数时,我们将得到如下错误(我还附上了错误截图,如下所示):
Microsoft.Csharp.RuntimeBinder.RuntimeBinderException:无法将类型“bool”隐式转换为“system.func”
左键的全部内容如下:
using ExcelDna.Integration;
using Excel = Microsoft.Office.Interop.Excel;
namespace Test_Atune
{
public class Dataset
{
public static string baseurl = Constant.URL_ROOT_COMMON;
public static string headval = Constant.HEADVAL_COMMON;
public static Request request = new Request();
[ExcelFunction(Category = "test", IsMacroType = true, Description = "test dataset")]
public static object TEST_DATASET(
[ExcelArgument(Description = "test_code")] string test_1,
[ExcelArgument(Description = "YYYYMMDD")] string test_2,
[ExcelArgument(Description = "YYYYMMDD")] string test_3
)
{
string parstr = @"/test_dataset/?" +
@"test_1=" + test_1 +
@"&test_2=" + test_2 +
@"&test_3=" + test_3;
ExcelReference caller = (ExcelReference)XlCall.Excel(XlCall.xlfCaller);
Excel.Range rg = caller.ToPiaRange();
return ExcelAsyncUtil.Run("TEST_DATASET",
new object[] { parstr },
() => request.GetDatasetFromUrl(Constant.URL_ROOT_COMMON, parstr, headval, rg));
}
}
}
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法
隐式地将类型“bool”转换为“system.func”在
目标(闭包、调用站点、对象)在
Test_AtuneAddIn.FundIndexTree.TreeView 1_NodeMouseDoubleClick(对象
发送者,树DemouseClickEventArgs(e)在
System.Windows.Forms.TreeView.OnNodeMouseDoubleClick(TreeNodeMouseClickEventArgs
(e)在 System.Windows.Forms.TreeView.WndProc(Message&m)在
System.Windows.Forms.NativeWindow.Callback(IntPtr-hWnd,Int32-msg,
IntPtr wparam、IntPtr lparam)
下面的代码是IndexTree.cs
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
using ExcelDna.Integration;
using System.Windows.Forms;
namespace Test_Atune
{
[ComVisible(true)]
public partial class FundIndexTree : UserControl
{
public IndexTree()
{
InitializeComponent();
}
RichText richText = new RichText();
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
Excel.Application ExcelApp = (Excel.Application)ExcelDnaUtil.Application;
//Excel.Dialog dialoddg = ExcelApp.Application.Dialogs[Excel.XlBuiltInDialog.xlDialogFunctionWizard];
// Return when there is child node
TreeNode node = e.Node;
if (node.Nodes.Count != 0 || !node.IsSelected)
{
return;
}
System.Func<dynamic> a;
// If there is no child node display the functions of pop-up dialog
Excel.Range activeCell = ExcelApp.ActiveCell;
if (node.Tag != null && node.Tag.ToString().Replace(" ", "").Length > 0)
{
if (activeCell.HasFormula)
{
if (activeCell.Formula.ToString().Substring(0, activeCell.Formula.ToString().IndexOf("(")).ToUpper().Replace(" ", "") ==
node.Tag.ToString().Substring(0, node.Tag.ToString().IndexOf("(")).ToUpper().Replace(" ", ""))
{
a = activeCell.FunctionWizard();
return;
}
}
activeCell.Value2 = node.Tag.ToString();
activeCell.FunctionWizard();
return;
}
else
{
MessageBox.Show("Coming soon!!");
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
this.richTextBox1.Text = richText.IndicatorDesc(e.Node.Text);
}
private void SplitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}
使用System.Runtime.InteropServices;
使用Excel=Microsoft.Office.Interop.Excel;
利用ExcelDna进行整合;
使用System.Windows.Forms;
名称空间测试
{
[ComVisible(true)]
公共部分类FundIndexTree:UserControl
{
公共索引树()
{
初始化组件();
}
RichText RichText=新的RichText();
私有void树视图1_NodeMouseDoubleClick(对象发送方,树DemouseClickEventArgs e)
{
Excel.Application ExcelApp=(Excel.Application)ExcelDnaUtil.Application;
//Excel.Dialog dialoddg=ExcelApp.Application.Dialogs[Excel.XlBuiltInDialog.xlDialogFunctionWizard];
//存在子节点时返回
树节点=e.节点;
如果(node.Nodes.Count!=0 | |!node.IsSelected)
{
返回;
}
系统功能a;
//如果没有子节点,则显示弹出对话框的功能
Excel.Range activeCell=ExcelApp.activeCell;
if(node.Tag!=null&&node.Tag.ToString().Replace(“,”).Length>0)
{
if(activeCell.HasFormula)
{
if(activeCell.Formula.ToString().Substring(0,activeCell.Formula.ToString().IndexOf(“(”).ToUpper().Replace(“,”)==
node.Tag.ToString()子字符串(0,node.Tag.ToString().IndexOf(“”).ToUpper().Replace(“”,“”)
{
a=activeCell.FunctionWizard();
返回;
}
}
activeCell.Value2=node.Tag.ToString();
activeCell.FunctionWizard();
返回;
}
其他的
{
MessageBox.Show(“即将到来!!”;
}
}
私有void treeView1_AfterSelect(对象发送方,treeviewventargs e)
{
this.richTextBox1.Text=richText.IndicatorDesc(e.Node.Text);
}
私有void SplitContainer1_Panel1_Paint(对象发送器,PaintEventArgs e)
{
}
}
}
我们必须让用户可以单击Excel函数来更改参数,而不会出现错误弹出窗口。也许这个问题可以帮助您:@RahulSharma我会尝试,非常感谢您分享~解决这个问题的运气如何?@RahulSharma很遗憾,我不知道它在哪里更改。您可以显示异常的调用堆栈吗?