3

有没有办法在导出到 Excel 函数中使用它从 UniversalSortableDateTimePattern 单独获取日期。Excel 不接受日期格式。
我需要显示为 yyyy-MM-dd。有什么建议会有帮助吗?我的代码为

 dtASalesDrivers.Columns.Add(new System.Data.DataColumn("Start Date", typeof(String)));
DateTime dt = Convert.ToDateTime(txtATrendStartDate.Text);
 drASalesDrivers[2] = string.Format("{0:yyyy-MM-dd}", dt);
dtASalesDrivers.Rows.Add(drASalesDrivers);

编辑 :

            DateTime dt = Convert.ToDateTime(txtATrendStartDate.Text);
            drASalesDrivers[2] = string.Format("{0:u}", dt);

这将显示日期和时间。有没有办法只显示日期???

4

2 回答 2

5

I think that is to do with your computers settings.

If you open up a new instance of Excel and type 2012-07-24 and navigate away from the cell, it will instantly change to 24/07/2012, it is more your computers regional settings than anything to do with your code.

You can however format the cell to yyyy-mm-dd;@ so you may need to do this programmatically and not change anything else, I think that this may work:

DateTime dt = Convert.ToDateTime(txtATrendStartDate.Text);
drASalesDrivers[2] = string.Format("{0:u}", dt);
drASalesDrivers[2].NumberFormat = "yyyy-mm-dd;@";

But I haven't tested it

I was doing some messing around and some example code is below:

using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;

namespace ConsoleApplication19
{
    class Program
    {
        static void Main(string[] args)
        {
            Application _excelApp = new Application();
            Workbook workbook = _excelApp.Workbooks.Open(@"C:\test\New Microsoft Excel Worksheet.xlsx");

            Worksheet sheet = (Worksheet)workbook.Sheets[1];
            Range excelRange = sheet.get_Range("A1");

            //If you comment the next line of code, you get '24/07/2012', if not you get '2012-07-24'
            excelRange.NumberFormat = "yyyy-mm-dd;@";

            excelRange.Value2 = "2012-07-13";

            workbook.Save();
            workbook.Close(false, @"C:\test\New Microsoft Excel Worksheet.xlsx", null);
            Marshal.ReleaseComObject(workbook);
        }
    }
}

Also, string.Format("{0:u}", dt); will indeed return a date and a time, dt.ToString("yyyy-MM-dd"); will return your date.

The alrernative is to change your Short date setting on your computer to yyy-MM-dd like so:

Date Settings

This will make your date appear how you want by default on Excel on your computer, but it will look differently on different computers.

于 2012-07-24T11:14:01.117 回答
1

这是我的代码,我通过以下代码实现:

 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
 Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
 Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
Microsoft.Office.Interop.Excel.Range excelRange;
excelRange = worksheet.get_Range("H2", System.Reflection.Missing.Value);
excelRange.NumberFormat = "yyyy-MM-dd;@";
于 2017-06-14T04:15:07.420 回答