Hello, I recently encountered the problem of processing large Excel files (about 160 MB, more than 560000, more than 26 columns). In general, I'm trying to load an Excel spreadsheet into a DataSet and process it. I use the following code:
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connectionString)) { conn.Open(); System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(); cmd.Connection = conn; DataTable dtSheet = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null); string sheetName = String.Empty; try { foreach (DataRow dr in dtSheet.Rows) { // dr["TABLE_NAME"] = "Table"; sheetName = dr["TABLE_NAME"].ToString(); // Get all rows from the Sheet cmd.CommandText = "SELECT * FROM [" + sheetName + "]"; DataTable dt = new DataTable(); dt.TableName = sheetName; System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(cmd); da.Fill(dt); ds.Tables.Add(dt); } } catch (Exception ex) { outputDataExcel(string.Format("Ошибка заполнения таблицы. Error:{0}", ex.Message)); } } On the line da.Fill(dt) an error occurs: "Not enough system resources.", Although the machine still has 9 GB of RAM, the process of filling the table ( da.Fill(dt) ) eats up to 2.3 GB of RAM.
Help solve the problem, tried to export data through Microsoft.Office.Interop.Excel , but it works very slowly due to accessing Excel cells.
Update
I tried to do this:
DataTable dataTable = new DataTable(); using (OleDbConnection connection = new OleDbConnection(connectionString)) { OleDbCommand command = new OleDbCommand("SELECT * FROM [Лист1$]", connection); connection.Open(); OleDbDataReader reader = command.ExecuteReader(); try { dataTable.Load(reader); } catch (Exception ex) { } while (reader.Read()) { Console.WriteLine(reader[0].ToString()); } reader.Close(); } Got the same error in the string
OleDbDataReader reader = command.ExecuteReader();
DataSetat once. Ship and process them one by one. Or even the first is not loaded? - Alexander Petrov