如何从excel文件中读取c#中的未来日期?

时间:2017-05-17 13:49:40

标签: c# excel date datagridview

   string filePath = openFileDialog1.FileName;
        string extension = Path.GetExtension(filePath);
        string header = rbHeaderYes.Checked ? "YES" : "NO";
        string conStr, sheetName;

        conStr = string.Empty;
        switch (extension)
        {

            case ".xls": //Excel 97-03
                conStr = string.Format(Excel03ConString, filePath, header);
                break;

            case ".xlsx": //Excel 07
                conStr = string.Format(Excel07ConString, filePath, header);
                break;
        }

        //Get the name of the First Sheet.
        using (OleDbConnection con = new OleDbConnection(conStr))
        {
            using (OleDbCommand cmd = new OleDbCommand())
            {
                cmd.Connection = con;
                con.Open();
                DataTable dtExcelSchema = 
   con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();

                con.Close();
            }
        }

        //Read Data from the First Sheet.
        using (OleDbConnection con = new OleDbConnection(conStr))
        {
            using (OleDbCommand cmd = new OleDbCommand())
            {
                using (OleDbDataAdapter oda = new OleDbDataAdapter())
                {
                    DataTable dt = new DataTable();
                  cmd.CommandText = "SELECT * From [" + sheetName + "]";

                   // cmd.CommandText = "SELECT * From ";

                    cmd.Connection = con;
                    con.Open();
                    oda.SelectCommand = cmd;
                    oda.Fill(dt);


                    //Populate DataGridView.

                    dataGridView1.DataSource = dt;
                    con.Close();
                }
            }
        }

这是我的excel文件..       enter image description here

我得到的是 enter image description here

在阅读/获取excel表时, 问题不在于阅读该年有31天的未来日期。喜欢在输出上显示。

我花了一整天的时间。我变得空白不知道发生了什么问题?

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

.Net没有尼泊尔日历的实现,正如您在System.Globalization的文档中看到的那样。

但是有一个nuget包似乎运作良好。 https://www.nuget.org/packages/NepaliDateConverter/

可以在github页面上找到实现细节 https://github.com/janaks09/NepaliDateConverter

安装此软件包后,您可以将英语翻译成尼泊尔语,反之亦然。

github页面的示例:

var convertedRawNepaliDate = new DateConverter().EngToNep(1993, 9, 30);

//English To Nepali date
//Separete properties for Year Month and Day.
var convertedNepaliDate = DateTime.Parse(convertedRawNepaliDate.ConvertedDate.Year.ToString() + "-" + convertedRawNepaliDate.ConvertedDate.Month.ToString() + "-" + convertedRawNepaliDate.ConvertedDate.Day.ToString());

var eqNepaliDay = convertedRawNepaliDate.ConvertedDayOfWeek; //Gives Nepali Day

//Nepali to english
var convertedRawEnglishDate = new DateConverter().NepToEng(2050, 06, 14);

var convertedNepaliDate = DateTime.Parse(convertedRawEnglishDate.ConvertedDate.Year.ToString() + "-" + convertedRawEnglishDate.ConvertedDate.Month.ToString() + "-" + convertedRawEnglishDate.ConvertedDate.Day.ToString());

var eqEnglishDay = convertedRawEnglishDate.ConvertedDayOfWeek; //Gives English Day