需要一种更好的方法从Excel工作表导入数据 [英] Need a Better Way of Importing Data from an Excel Worksheet

查看:71
本文介绍了需要一种更好的方法从Excel工作表导入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述


我正在创建Excel加载项这将操纵用户的活动工作簿。  当我将工作表读入内存时,我开始遇到"内存不足"的问题。当有数百万个用过的小区时出错。  关于更好的
方式的任何建议我都可以对此进行编码。



我的代码片段导入了"源数据"将工作表转换为多维数组。

 Excel.Worksheet source_Sheet =(Excel.Worksheet)this.Application.Worksheets [" Source Data"]; 
Excel.Range source_Range = source_Sheet.UsedRange;
object [,] source_Array =(object [,])source_Range.get_Value();

我希望将数据导入DataTable,这样我才能使用Linq。  同样,我的要求是代码运行时已经打开了工作簿。



如果您需要任何额外信息,请告诉我。信息。



谢谢


解决方案

回复:  excel范围和数组



我的2美分价值...

类型为Double(或Long)的数组使用更少的内存并且运行速度更快(在VBA中)比其他数据类型(字符串,对象,变体)。

一个"数百万"的数组。元素(在VBA中)成为船锚。

您必须将数据划分为更小的段并分别处理每个段。 这将是非常慢的。



车速表限制为200英里/小时,并不意味着汽车将会那么快。

在我的加载项,我通常会限制用户可以选择(或指定)的数据量。


'---

Jim Cone

美国俄勒冈州波特兰市
https://goo.gl/IUQUN2  (Dropbox)


Overview

I'm creating an Excel Add-In which will manipulate the active workbook for the user.  When I read worksheets into memory, I start running into "Out of Memory" errors when there are millions of used cells.  Any suggestions on a better way I can code this would be appreciated.

A snippet of my code which imports the "Source Data" worksheet into a multidimensional array.

Excel.Worksheet source_Sheet = (Excel.Worksheet)this.Application.Worksheets["Source Data"];
Excel.Range source_Range = source_Sheet.UsedRange;
object[,] source_Array = (object[,])source_Range.get_Value();

I'm hoping to import the data into a DataTable so I can use Linq.  Again, the requirement that I have is the workbook already be opened when the code runs.

Let me know if you need any additional information.

Thanks

解决方案

Re:  excel ranges and arrays

My 2 cents worth...
An array of type Double (or Long) uses less memory and runs faster (in VBA) than other data types (string, object, variant).
An array of "millions" of elements (in VBA) becomes a boat anchor.
You will have to divide the data into smaller segments and work on each one separately.  It will be very slow.

A speedometer limit of 200 mph, doesn't mean the auto will go that fast.
In my add-ins, I usually limit the amount of the data that the user can select (or specify).

'---

Jim Cone
Portland, Oregon USA
https://goo.gl/IUQUN2 (Dropbox)


这篇关于需要一种更好的方法从Excel工作表导入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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