How to make correct date format when writing data to Excel

后端 未结 10 1874
[愿得一人]
[愿得一人] 2020-11-30 04:56

Iam exporting a DataTable to an Excel-file using office interop. The problem is, that Excel does not recognize dates as such, but instead it displays numbers. In another cas

相关标签:
10条回答
  • 2020-11-30 05:06

    Expanding slightly on @Assaf answer, to apply formatting correctly I also had to convert the DateTime via the .ToOADate() function before the formatting took effect. You can do this on a cell by cell basis:

    xlWorkSheet.Cells[Row, Col].NumberFormat = "<Required Format>"; // e.g. dd-MMM-yyyy
    xlWorkSheet.Cells[Row, Col] = DateTimeObject.ToOADate();
    

    Or you can apply the formatting to the entire column:

    xlWorkSheet.Cells[Row, Col].EntireColumn.NumberFormat = "<Required Format>"; // e.g. dd-MMM-yyyy
    xlWorkSheet.Cells[Row, Col] = DateTimeObject.ToOADate();
    
    0 讨论(0)
  • 2020-11-30 05:07

    Did you try formatting the entire column as a date column? Something like this:

    Range rg = (Excel.Range)worksheetobject.Cells[1,1];
    rg.EntireColumn.NumberFormat = "MM/DD/YYYY";
    

    The other thing you could try would be putting a single tick before the string expression before loading the text into the Excel cell (not sure if that matters or not, but it works when typing text directly into a cell).

    0 讨论(0)
  • 2020-11-30 05:07

    Old question but still relevant. I've generated a dictionary that gets the appropriate datetime format for each region, here is the helper class I generated:

    https://github.com/anakic/ExcelDateTimeFormatHelper/blob/master/FormatHelper.cs

    FWIW this is how I went about it:

    1. opened excel, manually entered a datetime into the first cell of a workbook
    2. opened the regions dialog in control panel
    3. used Spy to find out the HWND's of the regions combobox and the apply button so I can use SetForegroundWindow and SendKey to change the region (couldn't find how to change region through the Windows API)
    4. iterated through all regions and for each region asked Excel for the NumberFormat of the cell that contained the date, saved this data to into a file
    0 讨论(0)
  • 2020-11-30 05:07

    This worked for me:

    hoja_trabajo.Cells[i + 2, j + 1] = fecha.ToString("dd-MMM-yyyy").Replace(".", "");
    
    0 讨论(0)
  • 2020-11-30 05:08

    I know this question is old but populating Excell Cells with Dates via VSTO has a couple of gotchas.

    • Formatting the entire column did NOT work for me.

    • Not even this approach from Microsoft worked: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.namedrange.numberformat.aspx

    I found Formula's don't work on dates with yyyy-mmm-dd format - even though the cells were DATE FORMAT! You have to translate Dates to a dd/mm/yyyy format for use in formula's.

    For example the dates I am getting come back from SQL Analysis Server and I had to flip them and then format them:

    using (var dateRn = xlApp.Range["A1"].WithComCleanup())
    {
        dateRn.Resource.Value2 = Convert.ToDateTime(dateRn.Resource.Value2).ToString("dd-MMM-yyyy");
    }
    
    
    using (var rn = xlApp.Range["A1:A10"].WithComCleanup())
    {
        rn.Resource.Select();
        rn.Resource.NumberFormat =  "d-mmm-yyyy;@";
    }
    

    Otherwise formula's using Dates doesn't work - the formula in cell C4 is the same as C3:

    enter image description here

    0 讨论(0)
  • 2020-11-30 05:12

    This worked for me:

    sheet.Cells[currentRow, ++currentColumn] = "'" + theDate.ToString("MM/dd/yy");
    

    Note the tick mark added before the date.

    0 讨论(0)
提交回复
热议问题