Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a 3,00,000 records Excel file, so I want to import records by 30,000 in each process. How to do that in C#?

I have used the OLEDB connection jet provider to import records but it imports records in one go in Datatable and sometimes causes error: out of memory to process Exception

What I have tried:

C#
string Excel03ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'";
string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'";

string conStr, sheetName;
conStr = string.Empty;

switch (extension)
{
    case ".xls": //Excel 97-03
        conStr = string.Format(Excel03ConString, fileName);
        break;
    case ".xlsx": //Excel 07
        conStr = string.Format(Excel07ConString, fileName);
        break;
}

//Get the name of the First Sheet.
using (OleDbConnection con = new OleDbConnection(conStr))
{
    using (OleDbCommand cmd = new OleDbCommand())
    {
        cmd.Connection = con;
        con.Open();
        DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
        con.Close();
    }
}

//Read Data from the First Sheet.
using (OleDbConnection con = new OleDbConnection(conStr))
{
    using (OleDbCommand cmd = new OleDbCommand())
    {
        using (OleDbDataAdapter oda = new OleDbDataAdapter())
        {
            cmd.CommandText = "SELECT * From [" + sheetName + "]";
            cmd.Connection = con;
            con.Open();
            oda.SelectCommand = cmd;
            //dt.Locale = CultureInfo.CurrentCulture;
            oda.Fill(dt);
            con.Close();
        }
    }
}
Posted
Updated 30-May-16 20:21pm
v2
Comments
Maciej Los 31-May-16 2:10am    
Please, Improve question and provide more details about your issue. You're receiving "out of memory to process..." error message in a piece of code you didn't post.
Sanjeev Legend 31-May-16 2:16am    
hi, #Maciej i was getting error when its fill the data in DATATABLE by OledbDataAdapter.
Actually i want to read a large Excel file by all 3 Lac or by Step by Step 30K. How i do that ?

1 solution

On the first look, the issue is in this line (underlined part):
C#
string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'";


For proper connection string for MS Excel 2007 and higher, please visit this site: Excel connection strings - ConnectionStrings.com[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900