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();
}