2

I need to read the excel data and store in database. when I try to read the data, there are some data got missed, I couldn't able to find.

It's my excel sheet enter image description here

My code :

private string Excel03ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";

 [![//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;
                        oda.Fill(dtExcelData);
                        con.Close();

                        //Populate DataGridView.
                        //dataGridView1.DataSource = dt;
                    }
                }
            }

            dtExcelData = dtExcelData.AsEnumerable().Skip(2).Take(1).CopyToDataTable();

            return dtExcelData;][2]][2]

Data after reading through in oledbConnection :

enter image description here

3
  • King, do you want to read all data (including headers) or do you want to get only data (without headers)? Commented May 28, 2016 at 8:34
  • 1
    Hi Mac,how are you, data is enough. Now it is working, after I set IMEX=1 in connection Commented May 28, 2016 at 8:37
  • 1
    Hi King! Using IMEX=1 is good practice, but you have to define the range you want to read. Please refer this Commented May 28, 2016 at 8:40

2 Answers 2

1

You have some merged cell in your excel Sheet , it's better to access your cells directly

I think here also can help you:

Excel - Reading Merged Cells (rows) in .NET using C#

Sign up to request clarification or add additional context in comments.

Comments

0

You can use this class to your project easily; https://exceldatareader.codeplex.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.