简体   繁体   English

将多维数组转换为具有可空值的单个数组

[英]Converting Multi-Dimensional Array to Single, with nullable Values

I am using ExcelDNA/C#/Excel primarily. 我主要使用ExcelDNA / C#/ Excel。 What I am essentially trying to do is convert a multi-dimensional array (namely a range of cells) to a singular dimensional array, using the following code: 我本质上想做的就是使用以下代码将多维数组(即一定范围的单元格)转换为单维数组:

private static string[] MultiToSingle(object[,] multiArray)
{
   List<string> tempList;
   string[] returnArray;
   tempList = new List<string>();

   //Add each element of the multi-dimensional Array to the list
   foreach (object oneObj in multiArray)
   {
      tempList.Add(oneObj.ToString());
   }
   //Convert the list to a single dimensional array
   returnArray = tempList.ToArray();
   return returnArray;
}

This works a treat, and is used a number of times throughout my project, however I would like to add some more functionality. 这很有效,并且在我的项目中多次使用,但是我想添加更多功能。

When I try to run this function with a range that contains an empty cell, it errors horribly, at the moment I just have a try/catch with an error message informing the user to enter N/A into any empty cells. 当我尝试在包含一个空单元格的范围内运行此功能时,它会严重错误,此刻,我只有一个try / catch并带有一条错误消息,通知用户在所有空单元格中输入N / A。

What I'd really like to do, is in this function perhaps, convert any 'null' or whatever Excel stores empty cells as to the text string "N/A". 我真正想做的是,也许是在此函数中,将任何“空”或任何存储空单元格的Excel转换为文本字符串“ N / A”。

Perhaps just: 也许只是:

tempList.Add(oneObj == null ? "n/a" : oneObj.ToString());

I can also think of ways to make it more efficient, if you want: 如果需要,我还可以考虑提高效率的方法:

string[] arr = new string[multiArray.Length];
int i = 0;
foreach (object oneObj in multiArray)
{
    arr[i++] = oneObj == null ? "n/a" : oneObj.ToString();
}
return arr;

This cuts out the intermediate list and a few backing array copies. 这样可以减少中间列表和一些后备阵列副本。

If you're finding nulls, which is probably why you're getting an error, Marc's answer is right. 如果您找到空值,这可能就是为什么您遇到错误,那么Marc的答案是正确的。 But you might like to use the function in another context - directly as a worksheet function exposed by Excel-DNA. 但是您可能想在另一个上下文中使用该函数-直接作为Excel-DNA公开的工作表函数。

Excel-DNA marshals empty Excel cells into the argument array as objects of type ExcelDna.Integration.ExcelEmpty. Excel-DNA将空的Excel单元格作为ExcelDna.Integration.ExcelEmpty类型的对象编组到参数数组中。 So if your code is called directly from Excel-DNA as a worksheet function, you'd get the type name from the ToString(), and not an error. 因此,如果直接从Excel-DNA作为工作表函数调用代码,则将从ToString()获得类型名称,而不是错误。

So your function with the return type changed to object[] (Excel-DNA won't register your string[] function directly) looks like this: 因此,将返回类型更改为object []的函数(Excel-DNA不会直接注册您的string []函数)如下所示:

public static object[] MultiToSingle(object[,] multiArray)
{
   List<string> tempList;
   object[] returnArray;
   tempList = new List<string>();

   //Add each element of the multi-dimensional Array to the list
   foreach (object oneObj in multiArray)
   {
      tempList.Add(oneObj.ToString());
   }
   //Convert the list to a single dimensional array
   returnArray = tempList.ToArray();
   return returnArray;
}

and gives this output in Excel when called directly as a worksheet function: 并在直接作为工作表函数调用时在Excel中提供以下输出: Excel-DNA数组功能

For this case you could add a check on the type of the array item to deal with empty cells: 对于这种情况,您可以在数组项的类型上添加检查以处理空单元格:

 tempList.Add( oneObj is ExcelEmpty ? "!EMPTY" : oneObj.ToString() ); 
tempList.Add( oneObj.Value == null ? "!EMPTY" : oneObj.ToString() ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM