传输到Excel时,日期格式会发生变化 [英] Date format changes when transferred to excel

查看:338
本文介绍了传输到Excel时,日期格式会发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将日期从DataGridView导出到excel而没有日期格式翻转,如果月份天数是12或更少。日期来自 03/09/2013 更改为 09/03/2016 。 howerver date 20/03/2016 不会更改为 03/20/2016



 oSheet.Range(EE).Value = DgvChequeData.Item( 成熟度,RNO).Value 







什么可能是问题吗?



谢谢



我尝试过:



我试过设置excel日期的格式但没有工作



oSheet.Range(EE ).Value = FormatDateTime(Trim(DgvChequeData.Item(Maturity,RNO).Value),DateFormat.ShortDate)

解决方案

这很可能是由于区域性设置。将源值转换为 DateTime 对象,并在Excel范围内使用 Value2 属性。

 oSheet.Range(EE).Value2 = Convert.ToDateTime(DgvChequeData.Item( 成熟度,RNO).Value)



如何:以编程方式在Excel范围中存储和检索日期值 [ ^ ]


您必须检查Excel单元格的数字格式。



Range.NumberFormat属性(Microsoft.Office.Interop .Excel) [ ^ ]

Range.NumberFormat Property(Excel) [ ^ ]



例如:

 ' 更改单元格的格式以将任何数据存储为文本。 
oSheet.Range(EE).NumberFormat = @





详情请见:自定义数字格式,日期& Excel中的时间格式和VBA; NumberFormat属性 [ ^ ]


oSheet.Range(EE).Value = DgvChequeData.Item(Maturity,RNO).Value

oSheet.Range(EE).NumberFormat =m / d / yyyy

I am having trouble exporting my date from a DataGridView to excel without the date format flipping around if the months days are 12 or less.i.e date from 03/09/2013 changes to 09/03/2016. howerver date 20/03/2016 does not change to 03/20/2016.

oSheet.Range(EE).Value = DgvChequeData.Item("Maturity", RNO).Value




What could be the issue?

Thanks

What I have tried:

I have tried setting the format for the excel date but not working

oSheet.Range(EE).Value =FormatDateTime(Trim(DgvChequeData.Item("Maturity", RNO).Value), DateFormat.ShortDate)

解决方案

It's most likely due to the regional settings. Convert the source value to a DateTime object, and use the Value2 property on the Excel range.

oSheet.Range(EE).Value2 = Convert.ToDateTime(DgvChequeData.Item("Maturity", RNO).Value)


How to: Programmatically Store and Retrieve Date Values in Excel Ranges[^]


You have to check the number format for Excel's cells.

Range.NumberFormat property (Microsoft.Office.Interop.Excel)[^]
Range.NumberFormat Property (Excel)[^]

For example:

'changes format of cell to store any data as a text.
oSheet.Range(EE).NumberFormat = "@"



For further details, please see: Custom Number Formats, Date & Time Formats in Excel & VBA; NumberFormat property [^]


oSheet.Range(EE).Value = DgvChequeData.Item("Maturity", RNO).Value
oSheet.Range(EE).NumberFormat = "m/d/yyyy"


这篇关于传输到Excel时,日期格式会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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