c#: ExcelDNA display an array in excel

2019-07-24 21:50发布

问题:

I'm creating a plugin for excel, i would like to know how is it possible to create new rows to display a list or array

I created a sample function :

[ExcelFunction(Description = "Split string", Category = "STRING")]
    public static object[] StringSplit([ExcelArgument(Name = "TEXT", Description = "text to split")] string text)
    {
        return text.Split(' ').ToArray();
    }

In this example, the function returns an array with the different keywords. but when i use it via excel, it displays only the first word in my cell. what i want to know is to create news columns or rows and display each element of my array in it. thanks

Edit I tried the solution posted by @CaioProiete bellow, so it works fine with this code :

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);

but when i try the asyncrohous method, this throw an exeption not handled :

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);
           });

the exception thrown is :

ExcelDna.Integration.XlCallException' occurred in ExcelDna.Integration.dll but was not handled in user code

And here is the line in the Reizer class that throw it:

ExcelReference caller = XlCall.Excel(XlCall.xlfCaller) as ExcelReference;

回答1:

Considering you're returning an array, you can span the results across columns by entering the formula in array mode.

Select the cells horizontally, type in the formula, and press CTRL+SHIFT+ENTER.

For example, selecting A1:C1 and typing =StringSplit("a b c") as an array formula, will display the three elements of the array in the three cells selected.

You need to know upfront how many elements will be returned and select the correct number of cells appropriately.

If you don't know how many elements will be returned, then you might want to look at Resizing Excel UDF result arrays, but it's a hack and should be avoided if possible.

Here is an example implementation of a helper macro that will resize your result array to the right size.


If you wanted to return rows and columns, you just need to return an object[,]

[ExcelFunction(Description = "Returns 2x2 matrix")]
public static object GetValues()
{
    return new object[,]
    {
        {1, 2},
        {3, 4},
    };
}

Everything else stays the same. You should return the number of elements that correspond to the number of cells selected by the user, unless you go with **resizing result arrays as per above.