Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem while reading data from excel in c#

Problem while reading data from excel in c#

Scheduled Pinned Locked Moved C#
helpcsharptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nainakarri
    wrote on last edited by
    #1

    Hi I have a code in c# to read data in excelsheet.

    string connstr = "provider = Microsoft.Jet.Oledb.4.0;Data Source='C:\\aaa.xls';Extended Properties=Excel 8.0;";
    OleDbConnection conn = new OleDbConnection(connstr);
    string strSQL = "SELECT * FROM [Sheet1$]";

            OleDbCommand cmd = new OleDbCommand(strSQL, conn);
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(ds);
            dt = ds.Tables\[0\];
    

    While reading i could not get few values in a particular column(say column B) and the values that are not being read are completely numeric(12345) while the others are alpha numeric(Ex: e0345) in the same column. Example Column B in excel has values as below U098ij i097hl 123453 gdtr56 When i see in the dataset, 3rd value is null. Dont know where i am going wrong. Please help me Thanks in advance Naina

    Naina

    W 1 Reply Last reply
    0
    • N nainakarri

      Hi I have a code in c# to read data in excelsheet.

      string connstr = "provider = Microsoft.Jet.Oledb.4.0;Data Source='C:\\aaa.xls';Extended Properties=Excel 8.0;";
      OleDbConnection conn = new OleDbConnection(connstr);
      string strSQL = "SELECT * FROM [Sheet1$]";

              OleDbCommand cmd = new OleDbCommand(strSQL, conn);
              DataSet ds = new DataSet();
              OleDbDataAdapter da = new OleDbDataAdapter(cmd);
              da.Fill(ds);
              dt = ds.Tables\[0\];
      

      While reading i could not get few values in a particular column(say column B) and the values that are not being read are completely numeric(12345) while the others are alpha numeric(Ex: e0345) in the same column. Example Column B in excel has values as below U098ij i097hl 123453 gdtr56 When i see in the dataset, 3rd value is null. Dont know where i am going wrong. Please help me Thanks in advance Naina

      Naina

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      There is a universal problem when accessing excel through ADO and that is that ADO only scans the first 8 lines and then chooses what data type to return based on the data type of the majority of the returned results. Thus if the majority of the returned results are strings, then it thinks that all the data should be returned as strings, and ignores other data types. The best way to get around this is to add the instruction IMEX=1 to the extended properties instruction in the connection string. Thus your connection string would now look like this:

      string connstr = @"provider = Microsoft.Jet.Oledb.4.0;Data Source='aaa.xls';Extended Properties='Excel 8.0;IMEX=1'";

      Of course, the problem with this is that all your data is returned as a string, and numeric values will need to be parsed. Hope this helps

      Live for today. Plan for tomorrow. Party tonight!

      M N 2 Replies Last reply
      0
      • W Wayne Gaylard

        There is a universal problem when accessing excel through ADO and that is that ADO only scans the first 8 lines and then chooses what data type to return based on the data type of the majority of the returned results. Thus if the majority of the returned results are strings, then it thinks that all the data should be returned as strings, and ignores other data types. The best way to get around this is to add the instruction IMEX=1 to the extended properties instruction in the connection string. Thus your connection string would now look like this:

        string connstr = @"provider = Microsoft.Jet.Oledb.4.0;Data Source='aaa.xls';Extended Properties='Excel 8.0;IMEX=1'";

        Of course, the problem with this is that all your data is returned as a string, and numeric values will need to be parsed. Hope this helps

        Live for today. Plan for tomorrow. Party tonight!

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Wayne Does this force large numbers into e##### format when converting to strings?

        Never underestimate the power of human stupidity RAH

        W 1 Reply Last reply
        0
        • M Mycroft Holmes

          Wayne Does this force large numbers into e##### format when converting to strings?

          Never underestimate the power of human stupidity RAH

          W Offline
          W Offline
          Wayne Gaylard
          wrote on last edited by
          #4

          Yes, unfortunately it does do this if you have the cell format set to general or number. This can lead to a serious loss of data. If you format the cells to currency or accounting, then you get the actual value.

          Live for today. Plan for tomorrow. Party tonight!

          1 Reply Last reply
          0
          • W Wayne Gaylard

            There is a universal problem when accessing excel through ADO and that is that ADO only scans the first 8 lines and then chooses what data type to return based on the data type of the majority of the returned results. Thus if the majority of the returned results are strings, then it thinks that all the data should be returned as strings, and ignores other data types. The best way to get around this is to add the instruction IMEX=1 to the extended properties instruction in the connection string. Thus your connection string would now look like this:

            string connstr = @"provider = Microsoft.Jet.Oledb.4.0;Data Source='aaa.xls';Extended Properties='Excel 8.0;IMEX=1'";

            Of course, the problem with this is that all your data is returned as a string, and numeric values will need to be parsed. Hope this helps

            Live for today. Plan for tomorrow. Party tonight!

            N Offline
            N Offline
            nainakarri
            wrote on last edited by
            #5

            Hi Thanks for your reply and now i have included IMEX to extended properties. Now the problem is, large numbers are converted into e**** format Regards Naina

            Naina

            W 1 Reply Last reply
            0
            • N nainakarri

              Hi Thanks for your reply and now i have included IMEX to extended properties. Now the problem is, large numbers are converted into e**** format Regards Naina

              Naina

              W Offline
              W Offline
              Wayne Gaylard
              wrote on last edited by
              #6

              Hi, please read my answer to Mycroft above. The only solution is to format your cells as accounting or currency.

              Live for today. Plan for tomorrow. Party tonight!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups