从Excel中读取的自定义日期格式 [英] custom date format reading from excel

查看:167
本文介绍了从Excel中读取的自定义日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的自定义日期格式是Excel单元格包含Mon-YYYY格式的日期值。

example = excel cell包含apr-14,但实际单元格值为28-04-2014,第二单元格值为apr-14,但值为13-04-2014。



但是我使用了函数以下方式:

  public   static  DateTime ? CleanDateField( string  DateField)
{

double d = < span class =code-keyword> double .Parse(DateField);

// 从OLE自动化日期获取转换日期。
DateTime conv = DateTime.FromOADate(d);


// 将文本转换为DateTime并返回值或null
DateTime? CleanDate = new DateTime();
int intDate;
bool DateIsInt = int .TryParse(DateField, out intDate);
if (DateIsInt)
{
// 如果这是一个序列日期,请将其转换为
CleanDate = DateTime.FromOADate(intDate);
}
else if (DateField.Length!= 0 && DateField!= 1/1/0001 12:00 :00 AM&&
DateField!= 1/1/1753 12 :00:00 AM
{
// 从一般转换格式
CleanDate =(Convert.ToDateTime(DateField));
}
其他
{
// < span class =code-comment>日期为空白
CleanDate = null ;
}
return CleanDate;
}





但我得到apr-14的单元格值是01-04-2014这是错误的我必须检索日期
导入时


1)第一个单元格值是28-04-2014

2)第二个单元格值是13-04- 2014



Plz帮助我搜索谷歌我没有找到正确的解决方案

提前感谢

解决方案

hi,

my custom date format is Excel cell contains date value in the format Mon-YYYY.
example = excel cell contain apr-14 but actual cell value is 28-04-2014 and second cell value apr-14 but value is 13-04-2014.

but i used function following ways:

public static DateTime? CleanDateField(string DateField)
    {

        double d = double.Parse(DateField);

        // Get the converted date from the OLE automation date.
        DateTime conv = DateTime.FromOADate(d);


        // Convert the text to DateTime and return the value or null
        DateTime? CleanDate = new DateTime();
        int intDate;
        bool DateIsInt = int.TryParse(DateField, out intDate);
        if (DateIsInt)
        {
            // If this is a serial date, convert it
            CleanDate = DateTime.FromOADate(intDate);
        }
        else if (DateField.Length != 0 && DateField != "1/1/0001 12:00:00 AM" &&
            DateField != "1/1/1753 12:00:00 AM")
        {
            // Convert from a General format
            CleanDate = (Convert.ToDateTime(DateField));
        }
        else
        {
            // Date is blank
            CleanDate = null;
        }
        return CleanDate;
    }



but i am getting cell values for apr-14 is 01-04-2014 which is wrong i have to retrieve date
values while importing
1) first cell value is 28-04-2014
2) second cell value is 13-04-2014

Plz help i search on google i did not find correct solution
thanks in advance

解决方案

这篇关于从Excel中读取的自定义日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆