将对象[,]转换为string []

时间:2014-02-12 17:49:29

标签: c# excel-interop

我有一张excel表返回c#中的Interop Excel范围。我要做的是抛出range.Value2,它以rangeVal2的形式返回[1,1] int [1,2] string [1,3] null。我想知道如何将Interop.range Value2转换为一维字符串数组[] 这是从手表返回Range.Value2

[1, 1] "Somestring" object {string}
[1, 2]  null    object
[1, 3]  null    object
[1, 4]  null    object
[1, 5]  0.0 object {double}
[1, 6]  0.0 object {double}
[1, 7]  0.0 object {double}

2 个答案:

答案 0 :(得分:1)

不确定你有什么,但试试

string[] arr = oRngListRow.Value2.Cast<object>()
    .Select(x => x == null ? null : x.ToString()).ToArray();

接近你在评论中所拥有的内容。

答案 1 :(得分:0)

请尝试以下方法。出于某种原因,Excel Interop不喜欢Linq。

        var list = new List<string>();

        for (int i = 1; i <= 7; i++)
        {
            list.Add((oRngListRow.Cells[1, i].Value2 ?? string.Empty).ToString());
        }
相关问题