A culture bug in WorksheetWriter.cs GetNumericDate

Jun 13, 2012 at 10:39 AM

Given that I am in Norway, our way of writing decimals is 12,21. Excel's internal representation of a date, assumes the 12.12 (. instead of ,).

 

So this method (last method in WorksheetWriter.cs):

  public static string GetNumericDate(DateTime date)
        {
            TimeSpan result = date - new DateTime(1900, 1, 1);
            int days = result.Days + 2; //Time difference + 2 

            double totalSeconds = 24.0F * 3600.0F;
            double fraction = ((date.Hour * 3600) + (date.Minute * 60) + date.Second) / totalSeconds; // Convert to a fraction of seconds in a day

            return (Convert.ToSingle(days) + fraction).ToString();
        }
gives 12,21 when run on a norwegian system, which then corrupts the Excel file.

A change of the last line to:
return (Convert.ToSingle(days) + fraction).ToString(System.Globalization.CultureInfo.InvariantCulture);
will give the correct representation in all cultures.

Nice project!