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

查看:337
本文介绍了从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循环做一个和字段的内容添加到多维数组。由于多维数组是对象类型[,]因此,非通用的,没有方便的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.

任何想法,如果这种方法是确定的,或者它可以做更多的efficientlty?

Any idea if this approach is ok or if it could be done more efficientlty?

非常感谢,

推荐答案

您可以阅读范围的值作为数组:

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
}

这code段是从微软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天全站免登陆