1

We are using OleDBConnection to export dates to an excel sheet but the first 12 days of each month are automatically changed to US format (MM/dd/yyyy).

This obviously screws with our data!

We checked it throughout our code and we know it gets changed only when we are creating the file and inserting the dates.

Is there a way to force the dates to be dd/MM/yyyy format?

ds.Tables[0].Rows[i][ds.Tables[0].Columns.Count - 1] is the date in the format dd/MM/yyyy hh:mm:ss

    System.Data.OleDb.OleDbConnection newconn = new System.Data.OleDb.OleDbConnection();
            try
            {
                string pathOfFileToCreate = "U:\\Visual Studio 2013\\Projects\\ANN\\FresnoDataCOC102-2.xlsx";
                newconn.ConnectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES"";", pathOfFileToCreate);
                newconn.Open();
                var cmd = newconn.CreateCommand();
                cmd.CommandText = "CREATE TABLE sheet1 (Date1 DATETIME, PanEObserved DOUBLE, PanECalculated DOUBLE)";
                cmd.ExecuteNonQuery();
                for (int i = 0; i < training; i++) // Sample Data Insert 
                {
                    int day = Convert.ToInt32(Convert.ToString(ds.Tables[0].Rows[i][ds.Tables[0].Columns.Count - 1]).Substring(0, 2));
                    int month = Convert.ToInt32(Convert.ToString(ds.Tables[0].Rows[i][ds.Tables[0].Columns.Count - 1]).Substring(3, 2));
                    int year = Convert.ToInt32(Convert.ToString(ds.Tables[0].Rows[i][ds.Tables[0].Columns.Count - 1]).Substring(6, 4));
                    DateTime date = new DateTime(year, month, day);

                    cmd.CommandText = String.Format("INSERT INTO Sheet1 (Date1, PanEObserved, PanECalculated) VALUES({0},{1},{2})", "#" + date + "#", ds.Tables[0].Rows[i][inputunits], outputs[i]);
                    cmd.ExecuteNonQuery(); // Execute insert query against excel file.
                }
            }
            finally
            {

                conn.Close();
            }
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Maybe using Date instead of datetime for your column type ? – cdie Feb 26 '16 at 10:42
  • We tried that but we're still having the same issue – Dan Haywood Mar 02 '16 at 13:25
  • Possible duplicate of [DateTime format mismatch on importing from Excel Sheet](https://stackoverflow.com/questions/965017/datetime-format-mismatch-on-importing-from-excel-sheet) – Johann May 21 '19 at 17:25

1 Answers1

0

A good workaround would be to use a string formatted instead:

System.Data.OleDb.OleDbConnection newconn = new System.Data.OleDb.OleDbConnection();
        try
        {
            string pathOfFileToCreate = "U:\\Visual Studio 2013\\Projects\\ANN\\FresnoDataCOC102-2.xlsx";
            newconn.ConnectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES"";", pathOfFileToCreate);
            newconn.Open();
            var cmd = newconn.CreateCommand();
            cmd.CommandText = "CREATE TABLE sheet1 (Date1 String/varhcar(100), PanEObserved DOUBLE, PanECalculated DOUBLE)"; //Check how to declare a varchar exactly.
            cmd.ExecuteNonQuery();
            for (int i = 0; i < training; i++) // Sample Data Insert 
            {
                int day = Convert.ToInt32(Convert.ToString(ds.Tables[0].Rows[i][ds.Tables[0].Columns.Count - 1]).Substring(0, 2));
                int month = Convert.ToInt32(Convert.ToString(ds.Tables[0].Rows[i][ds.Tables[0].Columns.Count - 1]).Substring(3, 2));
                int year = Convert.ToInt32(Convert.ToString(ds.Tables[0].Rows[i][ds.Tables[0].Columns.Count - 1]).Substring(6, 4));
                DateTime date = new DateTime(year, month, day);
                String dateAux = date.ToString("dd/MM/yyyy");
                cmd.CommandText = String.Format("INSERT INTO Sheet1 (Date1, PanEObserved, PanECalculated) VALUES({0},{1},{2})", "#" + dateAux + "#", ds.Tables[0].Rows[i][inputunits], outputs[i]);
                cmd.ExecuteNonQuery(); // Execute insert query against excel file.
            }
        }
        finally
        {

            conn.Close();
        }
Miquel Coll
  • 759
  • 15
  • 49