【发布时间】:2017-07-23 17:19:23
【问题描述】:
我有一个将 Excel 范围输出为object[,] 的方法:
static public Excel.Workbook OpenWorksheetAsReadOnly(string filePath)
{
Excel.Application excelInstance = null;
// Open workbook as readOnly, update links
excelInstance = new Excel.Application();
Excel.Workbook excelWorkbook = excelInstance.Workbooks.
Open(filePath, true, true);
return excelWorkbook;
}
static public object[,] ImportExcelIntoArray(string filePath)
{
Excel.Workbook source = null;
source = OpenWorksheetAsReadOnly(filePath);
Excel.Range sourceRange = source.Sheets[1].Range("B1:D1000");
object[,] sourceValues = (object[,])sourceRange.Value2;
return sourceValues;
}
我知道我的输出数组有 1000 行长。我也知道不是所有这些行都会被填充,而且我知道源文档是从上到下填充的。
在另一种方法中,我想遍历行,但为了节省时间,我只想遍历 实际填充 行的数量。理想情况下,我会使用(其中excelObject 是上述方法的输出):
int rowCount = excelObject.Count(x => x != null);
但是,它不适用于object[*,*] 类型——我认为它适用于List。我看过这里,也看到了excelObject.Length,但这只是给了我对象中的单元格总数。
有没有办法做到这一点?我需要将类型从object 更改为其他类型吗?单元格中的值既是字符串又是浮点数,所以我想尽可能长时间地保留它而不指定类型。
【问题讨论】: