How to change the FlexCel locale.
Some formats in Excel change depending on the locale of your machine. We have discussed those formats more in depth in the tip about internal numeric formats.
When FlexCel renders a file to pdf or other format, it needs to know how you want those locales printed. Is it dd/mm/yyyy or mm/dd/yyyy? As you would expect, if you don't do anything, FlexCel picks the locale from your machine settings. If you want to change how FlexCel renders those locale-dependent formats, the simplest way is to just change the machine settings. But sometimes you can't or don't want to change the machine locale, but still want FlexCel to render those formats as if the machine had some specific locale.
To change the locale without changing the machine locale, you can use the FormatSettings delphi global variable or the class .
You can change the locale in three different ways:
Change the locale only for the current thread, by using SetThreadFormat or PushThreadFormat. The difference between the two methods is that PushThreadFormat will save the original locale in the thread, so you can restore it after using it by calling PushThreadFormat
Thread format has the maximum priority: If you set the thread format, then this is what is going to be used by FlexCel, no matter what other settings you change. You can have different threads using different locales at the same time.
Change the Global locale, by using SetGlobalFormat . This option will change the locale for all FlexCel threads where you didn't set an explicit thread locale. If you call SetGlobalFormat and then SetThreadFormat for a specific thread, that thread will use the locale you specify in SetThreadFormat. This method still has more priority than changing the locale of your app with the FormatSettings Delphi global variable.
Change the locale of your full application by changing Delphi's FormatSettings variable. This will change the locale for the full application and this will include FlexCel, unless you use methods 2 or 1 to be more specific about what FlexCel should use.
Example:
var
SaveLocale: TFlexCelFormatSettings;
xls: TXlsFile;
pdf: TFlexCelPdfExport;
begin
SaveLocale := TFlexCelFormatSettings.PushThreadFormat('zh-CN', TFormatSettings.Create('zh-CN'));
try
xls := TXlsFile.Create('chinese.xlsx', true);
try
pdf := TFlexCelPdfExport.Create(xls, true);
try
pdf.Export('chinese.pdf');
finally
pdf.Free;
end;
finally
xls.Free;
end;
finally
TFlexCelFormatSettings.PopThreadFormat(SaveLocale);
end;
end;