简体   繁体   中英

C# Excel DateTime Export

I need to export some data to an Excel sheet. The data builds up from data time values and readings. The problem of mine that when I export the date time values they change their format to US dates instead of UK.

I tried to sort it by adding the format to the range where the date time values are, now it even gets funnier. I noticed that till month 9 it is handling the date time values right in UK format but after that it changes to US one.

After opening the excel file the format what I insert on cells by the application is applied only on the US formats.

Please see my code, it might give help to solve my problem:

        // Get dimensions of the 2-d array
        int rowCount = data2.GetLength(0);
        int columnCount = data2.GetLength(1);
        // Get an Excel Range of the same dimensions
        Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
        range = range.get_Resize(rowCount, columnCount);
        // Assign the 2-d array to the Excel Range
        range.set_Value(Microsoft.Office.Interop.Excel.XlRangeValueDataType.xlRangeValueDefault, data2);

        // Reset the range
        range = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
        range = range.get_Resize(rowCount, 1);
        // Set the format type of the range
        range.NumberFormat = "DD/MMM/YYYY hh:mm:ss";

EDIT SOLUTION With adrianm help here's the solution code:

I left the previously included code the same, but I changed the code which is creating the data2 object array (object[,] data2)

        //create the empty array
        var result = new object[data.Count, data[0].Count];
        //insert all of the values in it
        for (int i = 0; i < data.Count; i++)
        {
            for (int j = 0; j < data[i].Count; j++)
            {
                if (j < data[i].Count)
                    result[i, j] = data[i][j];
                else
                    result[i, j] = " ";
            }
        }

        //change the date time values to doubles which are 
        //in the first column after the first entry
        for (int i = 1; i < data.Count; i++)
        {
            result[i, 0] = (Convert.ToDateTime(result[i, 0])).ToOADate();
        }

            return result;

Try to format entire column as a date column like this

range.EntireColumn.NumberFormat = "DD/MM/YYYY";

Hope this will work

With adrianm help here's the solution code:

I left the previously included code the same, but I changed the code which is creating the data2 object array (object[,] data2)

    //create the empty array
    var result = new object[data.Count, data[0].Count];
    //insert all of the values in it
    for (int i = 0; i < data.Count; i++)
    {
        for (int j = 0; j < data[i].Count; j++)
        {
            if (j < data[i].Count)
                result[i, j] = data[i][j];
            else
                result[i, j] = " ";
        }
    }

    //change the date time values to doubles which are 
    //in the first column after the first entry
    for (int i = 1; i < data.Count; i++)
    {
        result[i, 0] = (Convert.ToDateTime(result[i, 0])).ToOADate();
    }

        return result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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