简体   繁体   English

如何读取包含超过 200,000 行的大型 excel 文件并将该数据加载到 C# 中的数据表中

[英]How to read large excel files containing more that 200,000 rows and load that data in datatable in C#

I am working on reading data from excel and loading it in datatable.我正在从 excel 读取数据并将其加载到数据表中。 My problem is that it is giving SystemOutOfMemoryException while loading large excel files.我的问题是它在加载大型 excel 文件时给出 SystemOutOfMemoryException。 The colomns in excel are not fixed so I can't load that data in sql table. excel 中的列不是固定的,所以我无法在 sql 表中加载该数据。 I need to do some manipulation on data so I am loading it in datatable.我需要对数据进行一些操作,所以我将它加载到数据表中。 Can anyone suggest me how to resolve this issue?谁能建议我如何解决这个问题?

I am doing it like this我这样做

 OleDbConnection conn = new OleDbConnection();
                OleDbCommand cmd = new OleDbCommand();
                OleDbDataAdapter da = new OleDbDataAdapter();
                conn = new OleDbConnection(GetOleDbConnectionString(strFileType, strNewPath));
                if (conn.State == ConnectionState.Closed) conn.Open();    
                string query = null;
                DataTable dt = new DataTable();              

                query = "SELECT  * FROM [" + SpreadSheetName + "]";               

                cmd.Connection = conn;
                cmd.CommandText = query;
                da.SelectCommand = cmd;
                da.Fill(dt);
                da.Dispose();
                conn.Close();
                conn.Dispose();

Your problem is not enough memory - likely your application runs as 32 bit app and all the stuff you load just is overloading it.您的问题是内存不足 - 可能您的应用程序作为 32 位应用程序运行,并且您加载的所有内容都使其过载。

Make it a 64 bit application (in settings under the executable project) - and make sure you have physical memory adequate for a modern machine (8+gb).使其成为 64 位应用程序(在可执行项目下的设置中) - 并确保您拥有适合现代机器(8+gb)的物理内存。

You are loading the data of whole excel sheet in memory, never do that, few environment variables come into scenario like available memory that may change if application is deployed in another machine, and columns filled in excel sheet - what if all of the columns are used with lengthy text then few thousand records could be enough to run out of memory.您正在内存中加载整个 excel 表格的数据,永远不要这样做,很少有环境变量进入场景,例如如果应用程序部署在另一台机器上可能会改变可用内存,并且在 Excel 表格中填充列 - 如果所有列都是与冗长的文本一起使用,那么几千条记录可能足以耗尽内存。

Better way is to fire a query to get only column name like更好的方法是触发查询以仅获取列名,例如

query = "SELECT  * FROM [" + SpreadSheetName + "]" where 1=2"

This will give you all the column names, use this to create a table in database.这将为您提供所有列名,使用它在数据库中创建一个表。 Once table is created either load records few at a time and do manipulation here on limited records and repeat till end.创建表后,要么一次加载少量记录,并在此处对有限的记录进行操作并重复直到结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM