YYYY-MM-DD日期格式导出到Excel [英] yyyy-MM-dd date format in Export to Excel

查看:1152
本文介绍了YYYY-MM-DD日期格式导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在出口以此来Excel函数从UniversalSortableDateTimePattern获得单独约会。在Excel不接受的日期格式..结果我需要显示为yyyy-MM-DD。
任何建议会有所帮助?
我的code作为

Is there any way for getting date alone from UniversalSortableDateTimePattern using this in Export to Excel function. The Excel doesn't accepts the date format..
I need to display as yyyy-MM-dd. Any suggestions ll be helpful ? My code as

 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);

这显示日期和时间。有什么办法来显示日期仅???

This displays both date and time. Is there any way for displaying only date ???

推荐答案

我认为这是与你的电脑设置的事。

I think that is to do with your computers settings.

如果你打开​​的Excel和键入 2012-07-24 和导航从小区离开一个新的实例,它会立即更改为 24/07/2012 ,它更是你的电脑比什么都与你的code区域设置。

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.

但是,您可以格式的单元格为 YYYY-MM-DD; @ 所以你可能需要以编程方式做到这一点,没有改变任何东西,我认为这可能工作

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;@";

但我没有测试它

我在做一些插科打诨和一些例如code下面是:

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);
        }
    }
}

此外,的String.Format({0:U},DT); 确实会返回一个日期和时间, DT。的ToString(YYYY-MM-DD); 将返回您的日期

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

该alrernative是改变你的短日期您的计算机上设置为YYY-MM-DD像这样:

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

这会使你的日期会出现你想怎么用Excel的默认您的计算机上,但它看起来不同在不同的计算机。

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

这篇关于YYYY-MM-DD日期格式导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆