简体   繁体   English

C#Excel VSTO无法将字符串转换为String.Array

[英]C# Excel VSTO can not convert string to String.Array

I am new to VSTO C# excel add-in. 我是VSTO C#excel外接程序的新手。 I am looking to find total count of not null/empty rows in a range. 我正在寻找范围内非空/空行的总数。 My Code looks at the range "A4:E4" and count total number of rows. 我的代码查看范围“ A4:E4”并计算总行数。

This is the code : 这是代码:

private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            Workbook workbook =  Globals.ThisAddIn.GetWorkBook("c:\\temp\\testfile.xlsx");
            Worksheet mergeSheet = workbook.Worksheets["Data"];
            Excel.Range mergeCells = mergeSheet.Range["A4:E4"];
            var colValues = (System.Array)mergeCells.Columns[1].Cells.Value;
            var strArray = colValues.OfType<object>().Select(o => o.ToString()).ToArray();
            var rowCount = strArray.Length;
        }

 [public Excel.Workbook GetWorkBook(string pathName)
        {
            return (Excel.Workbook)Application.Workbooks.Open(pathName);
        }][1]

I get error var colValues = (System.Array)mergeCells.Columns[1].Cells.Value; 我收到错误var colValues = (System.Array)mergeCells.Columns[1].Cells.Value; on line : 在线:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot convert type 'string' to 'System.Array''

It works when I have two rows in my range. 当我在范围内有两行时,它可以工作。 I have hardcoded range A4:E4 to produce the error. 我已经硬编码范围A4:E4来产生错误。 My excel sheet (testfile.xlsx) looks like below: 我的Excel工作表(testfile.xlsx)如下所示:

在此处输入图片说明

Any ideas how do I resolve this? 有什么想法我该如何解决?

Same line of code works when I have two rows. 当我有两行时,同一行代码有效。 Eg and following line is updated 例如,下一行已更新

Excel.Range mergeCells = mergeSheet.Range["A4:E5"];

在此处输入图片说明

This line is causing me trouble, var colValues = (System.Array)mergeCells.Columns[1].Cells.Value 这行给我带来麻烦, var colValues = (System.Array)mergeCells.Columns[1].Cells.Value

This row has only one value. 该行只有一个值。 Note that same line works when mergeCells range has two rows. 请注意,当mergeCells范围包含两行时,同一行有效。

Why it does not work for single cell range: 为什么它不适用于单个单元格范围:

The Value of a single cell is not an array (it's a Long, String, DateTime, etc.) and won't be cast to an array in that manner. Value的单电池的不是一个数组(它是一个长整型,字符串,日期时间等),并且将不被转换为以这种方式的阵列。 You can see this by testing like below: 您可以通过如下测试进行查看:

var myArray = (System.Array)"hello"

This will give same failure for other types: 对于其他类型,这将导致相同的失败:

在此处输入图片说明

Why it works for multi-cell range: 为什么适用于多单元范围:

The Value of a multi-cell range will return a variant array of the individual cell values, which either is, or can be cast to a System.Array Value多小区范围将返回个别单元格值的变型阵列,其或者是,或者可以转换为一个System.Array

There may be a better resolution, but at least you should be able to do like: 可能会有更好的分辨率,但是至少您应该能够做到:

var colValues;
if (mergeCells.Columns[1].Cells.Count > 1)
{
    colValues = (System.Array)mergeCells.Columns[1].Cells.Value;
}
else
{
    // NB: you may need to cast other data types to string
    //     or you could use .Cells[0].Text.Split()
    colValues = (System.Array)mergeCells.Columns[1].Cells[0].Value.Split();
}

The problem is that Range.Value can return different types of objects. 问题在于Range.Value可以返回不同类型的对象。 Among others, it can return 除其他外,它可以返回

  • a single value of type String , if the range contains a single cell containing a string or 类型为String的单个值(如果范围包含一个包含字符串的单个单元格,或者
  • an array of values, if the range contains more than one cell. 如果范围包含多个单元格,则为值的数组。

The simplest solution would be to count the number of cells and "wrap" the special "single value" case in an array: 最简单的解决方案是计算单元格的数量,并将特殊的“单值”情况“包装”在数组中:

var range = mergeCells.Columns[1].Cells;
var values = (range.Count == 1)
    ? new object[] { range.Value })
    : ((Sytem.Array)range.Value).Cast<object>();

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

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