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

查看:303
本文介绍了从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 efficientlty?

非常感谢,

推荐答案

您可以读取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
}

此代码段来自< a href =http://www.codeplex.com/msofficefornet =noreferrer>。MS Office的包装器。但同样的原则在于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天全站免登陆