Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I import data from Excel using ACE.OLEDB, the date format from is different for each column

My date column in excel comes as "16.03.2015" or "3/16/2015 12:00:00 AM" I need a converter to get the date Value and format it to "MM/dd/yyyy" format.

I read the data using the following code

C#
DataSet ds = new DataSet();
      string myConnStr = "";

      if (ds != null)
      {
       myConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +  FilePath     + "; " + "Extended Properties=\"Excel 12.0;HDR=YES\"";
      }
      else
      {
      myConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath +    ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
      }

      OleDbConnection myConn = new OleDbConnection(myConnStr);
      try
      {
          OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]  ",myConn);
          OleDbDataAdapter adapter = new OleDbDataAdapter();
          adapter.SelectCommand = cmd;
          myConn.Open();
          adapter.Fill(ds);
       }
      catch
      { }
      finally
      {
          myConn.Close();
      }



I can't loop the data and convert.
Posted
Updated 29-Mar-15 18:03pm
v2

1 solution

First of all, do not use IMEX=1 unless... Read this[^] to find out what IMEX switch means.

If you know the collection of columns, you're able to convert date to custom date format in SELECT statement:
SQL
SELECT Format([ColumnName], "dd/MM/yyyy") As MyDate
FROM [Sheet1$]


MS Access: Format Function (with Dates)[^]
 
Share this answer
 
v2
Comments
Rosarioraj 30-Mar-15 2:16am    
could you explain how to convert the column Please
Maciej Los 30-Mar-15 2:23am    
See updated 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