문제

Excel의 날짜 개체 인 스프레드 시트에 셀이 있지만 C1의 XLS 클래스에서 나올 때 두 배가됩니다 (2009 년 1 월 7 일 1/7/2009의 경우). 나는 이것이 줄리안 날짜 형식이라고 읽었다. 누군가 C#의 DateTime으로 다시 구문 분석하는 방법을 알려줄 수 있습니까?

업데이트 : 줄리안 날짜가 없을 수도 있고 1899 년 12 월 30 일 이후의 일수 인 것 같습니다.

도움이 되었습니까?

해결책

Excel이 표준 OLE 자동화를 사용하고 있다고 생각합니다. 데이트 로 변환 할 수있는 유형 DateTime.FromOADate 방법.

이 코드 블록,

using System;

namespace DateFromDouble
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.FromOADate(39820.0));
        }
    }
}

출력 :

1/7/2009 12:00:00 AM

다른 팁

System.globalization에는 Juliancalendar 클래스가 있습니다. 사용 방법은 다음과 같습니다.

            JulianCalendar c = new JulianCalendar();
            DateTime time = c.ToDateTime(2009, 1, 7, 0, 0, 0, 0);
            Console.WriteLine(time.ToShortDateString());

편집하다:

실제로 "1900"이후 며칠이라면 여기에 할 수있는 방법은 다음과 같습니다.

public static DateTime DaysSince1900(int days)
{
    return new DateTime(1900, 1, 1).AddDays(days);
}



 DateTime time = DaysSince1900(39820);
 Console.WriteLine(time.ToShortDateString()); //will result in "1/9/2009"

그 숫자는 '1900 년 이후의 날'값처럼 보입니다.

Excel 날짜를 다루는 경우 날짜는 날짜의 문자열 표현이거나 OA 날짜 일 수 있습니다. 이것은 날짜 변환을 용이하게하기 위해 잠시 후에 쓴 확장 방법입니다.

/// <summary>
/// Sometimes the date from Excel is a string, other times it is an OA Date:
/// Excel stores date values as a Double representing the number of days from January 1, 1900.
/// Need to use the FromOADate method which takes a Double and converts to a Date.
/// OA = OLE Automation compatible.
/// </summary>
/// <param name="date">a string to parse into a date</param>
/// <returns>a DateTime value; if the string could not be parsed, returns DateTime.MinValue</returns>
public static DateTime ParseExcelDate( this string date )
{
    DateTime dt;
    if( DateTime.TryParse( date, out dt ) )
    {
        return dt;
    }

    double oaDate;
    if( double.TryParse( date, out oaDate ) )
    {
        return DateTime.FromOADate( oaDate );
    }

    return DateTime.MinValue;
}

문제의 셀을 날짜로 서식하고 Ctrl+1을 사용하고 원하는 형식을 선택하십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top