2016-02-26 84 views
0

我們使用OleDBConnection將日期導出到Excel表,但每個月的前12天自動更改爲美國格式(MM/dd/yyyy)。c#OleDbConnection Excel設置日期格式

這顯然與我們的數據螺絲!

我們在整個代碼中檢查了它,我們知道只有當我們創建文件並插入日期時它纔會改變。

有沒有辦法強制日期爲dd/MM/yyyy格式?

ds.Tables[0].Rows[i][ds.Tables[0].Columns.Count - 1]格式爲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(); 
      } 
+0

也許使用日期,而不是日期時間爲您的列類型? – cdie

+0

我們嘗試過,但我們仍然有相同的問題 –

回答

0

一個好的解決辦法是使用代替格式化的字符串:

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

我們試過這個,但它沒有改變任何東西。仍然有同樣的問題 –