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. Read excel with diff datatypes in one column

Read excel with diff datatypes in one column

Scheduled Pinned Locked Moved C#
databasecsharpdata-structuresxmlworkspace
4 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.
  • S Offline
    S Offline
    Suman Singh
    wrote on last edited by
    #1

    Hi All, I have an excel file in this format, which I am trying to read using c# 04/05/2006 46 47 25 26 27 15 vgc 57 I am not able to read the values of row[1][0] and row[2][0], because in this column, the datatype of value in first row is date (04/05/2006). In the same way, I am not able to read the row[2][1], because in this column, the datatype of value in first row is a numeric. So, according to my perception, you need to have same datatype values in any particular column to read it in C#. I am looking for any option to read these type of excel files with values of different datatypes in a particular column. Any input will be highly appreciated. FYI, This is the code snippet, I am using to read this excel file. // Create connection string variable. Modify the "Data Source" parameter as appropriate for your environment. String filePath = "D:\\ProjectInfo\\HU Mail\\test.xls"; String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + filePath + "; Extended Properties=\"Excel 8.0;HDR=NO;\""; // Create connection object by using the preceding connection string. OleDbConnection objConn = new OleDbConnection(sConnectionString); // Open connection with the database. objConn.Open(); DataTable dt = null; // Get the data table containg the schema guid. dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); String[] strSheetName = new String[dt.Rows.Count]; int i = 0; // Add the sheet name to the string array. foreach (DataRow row in dt.Rows) { strSheetName[i] = row["TABLE_NAME"].ToString(); i++; } string strComand = "select * from [" + strSheetName[2] + "]"; OleDbCommand objCmdSelect = new OleDbCommand(strComand, objConn); // Create new OleDbDataAdapter that is used to build a DataSet based on the preceding SQL SELECT statement. OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); // Pass the Select command to the adapter. objAdapter1.SelectCommand = objCmdSelect; // Create new DataSet to hold information from the worksheet. DataSet dsDeal = new DataSet(); // Fill the DataSet with the information from the worksheet. objAdapter1.Fill(dsDeal, "LMPPrices"); for (int k = 0; k < dsDeal.Tables[0].Rows.Count; k++) { for (int j = 0; j < dsDeal.Tables[0].Columns.Count; j++) { Console.Write(dsDeal.Tables[0].Rows[k][j].ToString() + " -> "); } Console.WriteLine(); } Console.ReadLine(); Best Regards,

    J P 3 Replies Last reply
    0
    • S Suman Singh

      Hi All, I have an excel file in this format, which I am trying to read using c# 04/05/2006 46 47 25 26 27 15 vgc 57 I am not able to read the values of row[1][0] and row[2][0], because in this column, the datatype of value in first row is date (04/05/2006). In the same way, I am not able to read the row[2][1], because in this column, the datatype of value in first row is a numeric. So, according to my perception, you need to have same datatype values in any particular column to read it in C#. I am looking for any option to read these type of excel files with values of different datatypes in a particular column. Any input will be highly appreciated. FYI, This is the code snippet, I am using to read this excel file. // Create connection string variable. Modify the "Data Source" parameter as appropriate for your environment. String filePath = "D:\\ProjectInfo\\HU Mail\\test.xls"; String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + filePath + "; Extended Properties=\"Excel 8.0;HDR=NO;\""; // Create connection object by using the preceding connection string. OleDbConnection objConn = new OleDbConnection(sConnectionString); // Open connection with the database. objConn.Open(); DataTable dt = null; // Get the data table containg the schema guid. dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); String[] strSheetName = new String[dt.Rows.Count]; int i = 0; // Add the sheet name to the string array. foreach (DataRow row in dt.Rows) { strSheetName[i] = row["TABLE_NAME"].ToString(); i++; } string strComand = "select * from [" + strSheetName[2] + "]"; OleDbCommand objCmdSelect = new OleDbCommand(strComand, objConn); // Create new OleDbDataAdapter that is used to build a DataSet based on the preceding SQL SELECT statement. OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); // Pass the Select command to the adapter. objAdapter1.SelectCommand = objCmdSelect; // Create new DataSet to hold information from the worksheet. DataSet dsDeal = new DataSet(); // Fill the DataSet with the information from the worksheet. objAdapter1.Fill(dsDeal, "LMPPrices"); for (int k = 0; k < dsDeal.Tables[0].Rows.Count; k++) { for (int j = 0; j < dsDeal.Tables[0].Columns.Count; j++) { Console.Write(dsDeal.Tables[0].Rows[k][j].ToString() + " -> "); } Console.WriteLine(); } Console.ReadLine(); Best Regards,

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      This is kind of a hack-ish workaround, but how about reading every cell as a string and then using int.TryParse and DateTime.TryParse to determine what type each particular cell is?

      1 Reply Last reply
      0
      • S Suman Singh

        Hi All, I have an excel file in this format, which I am trying to read using c# 04/05/2006 46 47 25 26 27 15 vgc 57 I am not able to read the values of row[1][0] and row[2][0], because in this column, the datatype of value in first row is date (04/05/2006). In the same way, I am not able to read the row[2][1], because in this column, the datatype of value in first row is a numeric. So, according to my perception, you need to have same datatype values in any particular column to read it in C#. I am looking for any option to read these type of excel files with values of different datatypes in a particular column. Any input will be highly appreciated. FYI, This is the code snippet, I am using to read this excel file. // Create connection string variable. Modify the "Data Source" parameter as appropriate for your environment. String filePath = "D:\\ProjectInfo\\HU Mail\\test.xls"; String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + filePath + "; Extended Properties=\"Excel 8.0;HDR=NO;\""; // Create connection object by using the preceding connection string. OleDbConnection objConn = new OleDbConnection(sConnectionString); // Open connection with the database. objConn.Open(); DataTable dt = null; // Get the data table containg the schema guid. dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); String[] strSheetName = new String[dt.Rows.Count]; int i = 0; // Add the sheet name to the string array. foreach (DataRow row in dt.Rows) { strSheetName[i] = row["TABLE_NAME"].ToString(); i++; } string strComand = "select * from [" + strSheetName[2] + "]"; OleDbCommand objCmdSelect = new OleDbCommand(strComand, objConn); // Create new OleDbDataAdapter that is used to build a DataSet based on the preceding SQL SELECT statement. OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); // Pass the Select command to the adapter. objAdapter1.SelectCommand = objCmdSelect; // Create new DataSet to hold information from the worksheet. DataSet dsDeal = new DataSet(); // Fill the DataSet with the information from the worksheet. objAdapter1.Fill(dsDeal, "LMPPrices"); for (int k = 0; k < dsDeal.Tables[0].Rows.Count; k++) { for (int j = 0; j < dsDeal.Tables[0].Columns.Count; j++) { Console.Write(dsDeal.Tables[0].Rows[k][j].ToString() + " -> "); } Console.WriteLine(); } Console.ReadLine(); Best Regards,

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Take a look at http://www.codeproject.com/useritems/Excel_Application_in_C_.asp[^]

        1 Reply Last reply
        0
        • S Suman Singh

          Hi All, I have an excel file in this format, which I am trying to read using c# 04/05/2006 46 47 25 26 27 15 vgc 57 I am not able to read the values of row[1][0] and row[2][0], because in this column, the datatype of value in first row is date (04/05/2006). In the same way, I am not able to read the row[2][1], because in this column, the datatype of value in first row is a numeric. So, according to my perception, you need to have same datatype values in any particular column to read it in C#. I am looking for any option to read these type of excel files with values of different datatypes in a particular column. Any input will be highly appreciated. FYI, This is the code snippet, I am using to read this excel file. // Create connection string variable. Modify the "Data Source" parameter as appropriate for your environment. String filePath = "D:\\ProjectInfo\\HU Mail\\test.xls"; String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + filePath + "; Extended Properties=\"Excel 8.0;HDR=NO;\""; // Create connection object by using the preceding connection string. OleDbConnection objConn = new OleDbConnection(sConnectionString); // Open connection with the database. objConn.Open(); DataTable dt = null; // Get the data table containg the schema guid. dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); String[] strSheetName = new String[dt.Rows.Count]; int i = 0; // Add the sheet name to the string array. foreach (DataRow row in dt.Rows) { strSheetName[i] = row["TABLE_NAME"].ToString(); i++; } string strComand = "select * from [" + strSheetName[2] + "]"; OleDbCommand objCmdSelect = new OleDbCommand(strComand, objConn); // Create new OleDbDataAdapter that is used to build a DataSet based on the preceding SQL SELECT statement. OleDbDataAdapter objAdapter1 = new OleDbDataAdapter(); // Pass the Select command to the adapter. objAdapter1.SelectCommand = objCmdSelect; // Create new DataSet to hold information from the worksheet. DataSet dsDeal = new DataSet(); // Fill the DataSet with the information from the worksheet. objAdapter1.Fill(dsDeal, "LMPPrices"); for (int k = 0; k < dsDeal.Tables[0].Rows.Count; k++) { for (int j = 0; j < dsDeal.Tables[0].Columns.Count; j++) { Console.Write(dsDeal.Tables[0].Rows[k][j].ToString() + " -> "); } Console.WriteLine(); } Console.ReadLine(); Best Regards,

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          And there's a comment in http://www.codeproject.com/office/excel_using_oledb.asp[^] that says Apparently, the engine reads the first 8 cells of each column and check it's data type. if most of the first 8 cells are int / double, the problem remains.

          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