简体   繁体   English

C#OleDbConnection Excel设置日期格式

[英]c# OleDbConnection Excel set date format

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). 我们使用OleDBConnection将日期导出到Excel工作表,但是每个月的前12天会自动更改为美国格式(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? 有没有一种方法可以强制将日期设置为dd / MM / yyyy格式?

ds.Tables[0].Rows[i][ds.Tables[0].Columns.Count - 1] is the date in the format dd/MM/yyyy hh:mm:ss 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();
            }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM