传输到Excel时,日期格式会发生变化 [英] Date format changes when transferred to 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单元格的数字格式。
Range.NumberFormat属性(Microsoft.Office.Interop .Excel) [ ^ ]
Range.NumberFormat Property(Excel) [ ^ ]
例如:
' 更改单元格的格式以将任何数据存储为文本。
oSheet.Range(EE).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 aDateTime
object, and use theValue2
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屋!