c#:ExcelDNA 在 excel 中显示一个数组
c#: ExcelDNA display an array in excel
我正在为 excel 创建一个插件,我想知道如何创建新行来显示列表或数组
我创建了一个示例函数:
[ExcelFunction(Description = "Split string", Category = "STRING")]
public static object[] StringSplit([ExcelArgument(Name = "TEXT", Description = "text to split")] string text)
{
return text.Split(' ').ToArray();
}
在这个例子中,函数 returns 一个包含不同关键字的数组。但是当我通过 excel 使用它时,它只显示我单元格中的第一个单词。
我想知道的是创建新闻栏或行并在其中显示数组的每个元素。
谢谢
编辑
我尝试了@C 发布的解决方案。 Augusto Proiete 波纹管,所以它适用于此代码:
Page page = new Page();
WebResponse resp = page.Download(url);
List<string> res = page.GetAllFromXpath(resp.content, xpath);
object[,] result = new object[1, res.Count];
for (int j = 0; j < res.Count; j++)
{
result[0, j] = res.ElementAt(j);
}
return Resizer.Resize(result);
但是当我尝试异步方法时,这会抛出一个未处理的异常:
return ExcelAsyncUtil.Run("DownloadAsync", url,
delegate
{
Page page = new Page();
WebResponse resp = page.Download(url);
List<string> res = page.GetAllFromXpath(resp.content, xpath);
object[,] result = new object[1, res.Count];
for (int j = 0; j < res.Count; j++)
{
result[0, j] = res.ElementAt(j);
}
return Resizer.ResizeMe(result);
});
抛出的异常是:
ExcelDna.Integration.XlCallException' occurred in ExcelDna.Integration.dll but was not handled in user code
这是 Reizer class 中抛出它的行:
ExcelReference caller = XlCall.Excel(XlCall.xlfCaller) as ExcelReference;
考虑到您正在 return 数组,您可以通过在数组模式下输入公式来跨列结果。
Select水平单元格,输入公式,然后按CTRL
+SHIFT
+ENTER
.
例如,selecting A1:C1
并键入 =StringSplit("a b c")
作为数组公式,将在 selected 的三个单元格中显示数组的三个元素。
您需要预先知道有多少元素将 returned 和 select 正确的单元格数量。
如果您不知道有多少元素将被 returned,那么您可能想查看 Resizing Excel UDF result arrays,但这是一个 hack并且应该尽可能避免。
Here is an example implementation 一个辅助宏,可以将结果数组的大小调整到合适的大小。
如果你想要 return 行和列,你只需要 return 一个 object[,]
[ExcelFunction(Description = "Returns 2x2 matrix")]
public static object GetValues()
{
return new object[,]
{
{1, 2},
{3, 4},
};
}
其他一切都保持不变。您应该 return 元素的数量对应于用户 select 编辑的单元格数量,除非您按照上面的方式使用 **resizing result arrays。
我正在为 excel 创建一个插件,我想知道如何创建新行来显示列表或数组
我创建了一个示例函数:
[ExcelFunction(Description = "Split string", Category = "STRING")]
public static object[] StringSplit([ExcelArgument(Name = "TEXT", Description = "text to split")] string text)
{
return text.Split(' ').ToArray();
}
在这个例子中,函数 returns 一个包含不同关键字的数组。但是当我通过 excel 使用它时,它只显示我单元格中的第一个单词。 我想知道的是创建新闻栏或行并在其中显示数组的每个元素。 谢谢
编辑 我尝试了@C 发布的解决方案。 Augusto Proiete 波纹管,所以它适用于此代码:
Page page = new Page();
WebResponse resp = page.Download(url);
List<string> res = page.GetAllFromXpath(resp.content, xpath);
object[,] result = new object[1, res.Count];
for (int j = 0; j < res.Count; j++)
{
result[0, j] = res.ElementAt(j);
}
return Resizer.Resize(result);
但是当我尝试异步方法时,这会抛出一个未处理的异常:
return ExcelAsyncUtil.Run("DownloadAsync", url,
delegate
{
Page page = new Page();
WebResponse resp = page.Download(url);
List<string> res = page.GetAllFromXpath(resp.content, xpath);
object[,] result = new object[1, res.Count];
for (int j = 0; j < res.Count; j++)
{
result[0, j] = res.ElementAt(j);
}
return Resizer.ResizeMe(result);
});
抛出的异常是:
ExcelDna.Integration.XlCallException' occurred in ExcelDna.Integration.dll but was not handled in user code
这是 Reizer class 中抛出它的行:
ExcelReference caller = XlCall.Excel(XlCall.xlfCaller) as ExcelReference;
考虑到您正在 return 数组,您可以通过在数组模式下输入公式来跨列结果。
Select水平单元格,输入公式,然后按CTRL
+SHIFT
+ENTER
.
例如,selecting A1:C1
并键入 =StringSplit("a b c")
作为数组公式,将在 selected 的三个单元格中显示数组的三个元素。
您需要预先知道有多少元素将 returned 和 select 正确的单元格数量。
如果您不知道有多少元素将被 returned,那么您可能想查看 Resizing Excel UDF result arrays,但这是一个 hack并且应该尽可能避免。
Here is an example implementation 一个辅助宏,可以将结果数组的大小调整到合适的大小。
如果你想要 return 行和列,你只需要 return 一个 object[,]
[ExcelFunction(Description = "Returns 2x2 matrix")]
public static object GetValues()
{
return new object[,]
{
{1, 2},
{3, 4},
};
}
其他一切都保持不变。您应该 return 元素的数量对应于用户 select 编辑的单元格数量,除非您按照上面的方式使用 **resizing result arrays。