OLE Excel输出中的国际date格式问题
在目前使用Excel的产品中,电子表格是使用Delphi的OLE生成的。 电子表格中包含许多应用程序从系统设置中加载shortdate格式并将其应用于单元格的date。 除非系统格式设置为至less俄语,巴什基尔,鞑靼语,雅库特,哈萨克和乌兹别克语(可能还有其他我没有尝试过的语言)之一,否则这种做法是完美的。
代码是 –
xlws.Cells[irow,icol] := ActivityData.Target_Date; xlvalidrange := xlws.Range[xlws.Cells[irow,icol],xlws.Cells[irow,icol]]; xlvalidrange.NumberFormat:= LocShortDateFormat;
在这种情况下(同时debugging时)ActivityData.Target_Date是41192,这是Excel存储date和时间的格式,LocShortDateFormat等于“dd.MM.yyyy”。 这与俄罗斯的Windows中显示的商店date格式相匹配,并以代码方式以编程方式检索,
LocShortDateFormat := ShortDateFormat;
以这种格式打开电子表格显示消息 –
Excel在Spreadsheet.xlsx中发现无法读取的内容是否要恢复此工作簿的内容?
这样做的数据显示简单的单元格中没有NO格式的数字41192。 更奇怪的是,手动应用格式string“dd.MM.yyyy”,然后在单元格中显示“dd.MM.yyyy”而不是数据。
这同样适用于任何标准date/时间string格式化字符。 通过select“date”类别和相应types作为格式,数据可以手动更改为date格式,然后正确显示,所以这不是数据问题,而是格式string。
Excel手动select正确的date格式时使用的格式string是ДД.ММ.ГГГГ – 手动更改debugging时使用的格式string会在生成的电子表格中产生正确的输出,并且不可读的内容消息将消失。
奇怪的是,中文,日文,塞尔维亚文和马其顿文(作为非英文字符集的样本)都使用标准的date/时间格式字符。
在Excel本身中,即使电子表格是新的,并且还没有以编程方式创build,即在Excel应用程序内创build的新电子表格,标准字符似乎也不能正常工作。
这种情况下,它似乎不是一个Delphi或Windows的问题(C#Windows窗体没有问题,正确格式化date使用标准字符,即使俄语区域设置),但它仍然留下如何得到返回正确的格式Delphi中的string。
我并不热衷于对已知有问题的语言环境进行一系列有条件的“if”语句testing,但这似乎是基于迄今为止我所做的研究来处理这个问题的唯一方法。
有没有办法让Excel使用通过OLE的本地date格式化,而不提供格式化string,或者获取stringExcel用于格式化本地化date以便提供正确格式的string?
是的,您可以使用Excel应用程序实例上的“ International
属性获取各种国际化/本地化字符和string。 后期的例子:
function GetExcel_International(const aExcel: OleVariant; const aIndex: Integer): string; begin try Result := aExcel.International[aIndex]; except ... end; end;
我们用它来获取组成格式化string的各种字符:
ExcelYearCharacter := GetExcel_International(FExcelApplication, xlYearCode); ExcelMonthCharacter := GetExcel_International(FExcelApplication, xlMonthCode); ExcelDayCharacter := GetExcel_International(FExcelApplication, xlDayCode); ExcelHourCharacter := GetExcel_International(FExcelApplication, xlHourCode); ExcelMinuteCharacter := GetExcel_International(FExcelApplication, xlMinuteCode);
然后,我们使用这些从Windows转换到Excel格式:
function ConvertWindowsLocalDateStringToExcel(const aString: string): string; begin Result := StringReplace(aString, UpperCase(WindowsYearCharacter), UpperCase(ExcelYearCharacter), [rfReplaceAll]); Result := StringReplace(Result, LowerCase(WindowsYearCharacter), LowerCase(ExcelYearCharacter), [rfReplaceAll]); Result := StringReplace(Result, UpperCase(WindowsMonthCharacter), UpperCase(ExcelMonthCharacter), [rfReplaceAll]); Result := StringReplace(Result, LowerCase(WindowsMonthCharacter), LowerCase(ExcelMonthCharacter), [rfReplaceAll]); Result := StringReplace(Result, UpperCase(WindowsDayCharacter), UpperCase(ExcelDayCharacter), [rfReplaceAll]); Result := StringReplace(Result, LowerCase(WindowsDayCharacter), LowerCase(ExcelDayCharacter), [rfReplaceAll]); end;
所有的xl...
常量来自我们自己的单位之一,但是你也应该能够在早期绑定的Delphi OLE服务器包装器中find它们。 例如,在Delphi / RAD Studio安装的...\OCX\Servers
文件夹中的ExcelXP
单元中。
有关Application.International
属性的更多信息,请参阅: http : //msdn.microsoft.com/en-us/library/office/bb177675(v=office.12).aspx
我的解决scheme如下:
首先我做一个typesTDate
的var
。 然后我从表格中填充var
,然后插入它,就像这样:
var tReportDate : TDate; ....<br><br> tReportDate := FieldByName('DateReport').AsDateTime;<br> ExcelApp.Cells[StartColumnid, 1].Value := tReportDate;
这解决了我的情况。 这是一个国际项目,并在各种地点使用。
希望这可以帮助。