从 Excel 读取(范围到多维数组)C# [英] Reading from Excel (Range into multidimensional Array) C#

查看:26
本文介绍了从 Excel 读取(范围到多维数组)C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从 Excel 工作表中读取并将标记的选择(区域)加载到多维数组中?Excel 中的一列本身可以是一个多维数组,因为它包含多个值.

How would I read from an Excel sheet and load the marked selection (Area) into an multidimensional array? A column in Excel could itself be a multi dimensional array since it would contain more than just one value.

现在的想法(不知道这是好是坏)是在所有 Excel.Area(选定字段)中执行 for 循环,并将该字段的内容添加到多维数组中.由于多维数组是 object[,] 类型,因此是非泛型的,因此没有方便的 add() 方法.所有这些都需要手动完成.

The idea (not sure how good or bad this is) is right now is to do a for loop through all the Excel.Area (selected fields) and add the content of that field to the multi dimensional array. Since the multi dimensional array is of type object[,] and therefore non-generic there is no convenient add() method to it. All of it needs to be done manually.

不知道这种方法是否可行,或者是否可以更有效地完成?

Any idea if this approach is OK or if it could be done more efficiently?

推荐答案

可以将 Range 的值读取为数组:

You can read the value of Range as array:

using (MSExcel.Application app = MSExcel.Application.CreateApplication()) 
{
    MSExcel.Workbook book1 = app.Workbooks.Open( this.txtOpen_FilePath.Text);
    MSExcel.Worksheet sheet = (MSExcel.Worksheet)book1.Worksheets[1];
    MSExcel.Range range = sheet.GetRange("A1", "F13");

    object value = range.Value; //the value is boxed two-dimensional array
}

此代码片段来自 MS Office 的 .NET 包装器.但同样的原理在 MS Excel 中的 VSTO 或 VBA 中.

This code snippet is from .NET wrapper for MS Office. But same princip is in VSTO or VBA in MS Excel.

这篇关于从 Excel 读取(范围到多维数组)C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆