Hi,
I see 2 issues here:
1) While you are using the correct expected format, the string "22112013" is also a number. SetCellFromString will try to convert the string to the "best" format, and in this case it will just convert it to a number. (as numbers are tried before dates, SetCellFromString will see this is a number, enter it, and the code to test for dates won't even be called).
In order for this to work you would need some "date separator" or something in the string, so it isn't a number. For example "22-112013" will work fine.
2)While vIndFormat specifies most of the format for the cell, in this particular case FlexCel will ignore the formatting string, since it has to change it depending if it is a number, bool, etc. So for this to work, you would have to set the full format again in next line.
This code should get you what you want:
TXlsFile* pXls = new TXlsFile(1, true);
TFlxFormat viMyFormat = pXls->GetDefaultFormat;
viMyFormat.Format = "dd-MM-yy";
int viIndFormat = pXls->AddFormat(viMyFormat);
DynamicArray<String>ExpectedDateFormat;
ExpectedDateFormat.Length = 1;
ExpectedDateFormat[0] = "dd-MMyyyy";
pXls->SetCellFromString(1, 1, "22-112013", viIndFormat, ExpectedDateFormat);
pXls->SetCellFormat(1, 1, viIndFormat);
pXls->Save("r:\test.xlsx");
Now, I am not sure if this is the best approach. SetCellFromString is designed to deal with cases when you only have a string and don't know what datatype it is. SetCellFromString has to apply heuristics to "guess" what datatype it is, and it might not be correct 10% of the times. As in this case, where it sees the string "22112013" and it can't know if it is supposed to be a number or a date.
But you do know this is a date and not a number, so probably the best is that you set the value as a date directly. No need to have FlexCel guess what datatype it is.
I would personally use some code like this:
#include <__UMiscUtils.hpp>
...
void __fastcall TForm11::Button1Click(TObject Sender)
{
TXlsFile pXls = new TXlsFile(1, true);
TFlxFormat viMyFormat = pXls->GetDefaultFormat;
viMyFormat.Format = "dd-MM-yy";
int viIndFormat = pXls->AddFormat(viMyFormat);
DynamicArray<String>ExpectedDateFormat;
ExpectedDateFormat.Length = 1;
ExpectedDateFormat[0] = "ddMMyyyy";
TDateTime Date;
__Umiscutils::ConvertDateToNumber("22112013", ExpectedDateFormat, Date);
pXls->SetCellValue(1, 1, TCellValue::Create(Date), viIndFormat);
pXls->Save("r:\test.xlsx");
}
Note that I am using an "internal" FlexCel method to convert from a string to a date given a format:
__Umiscutils::ConvertDateToNumber(...)
and this is why I also had to include __UMiscUtils.hpp
I used this because it is what FlexCel uses internally, but you could use standard VCL or C++ methods to convert a string to a date if you prefer. (I am not really sure on what those methods are, I would have to check)
Syntax also get a little funny in C++ builder when dealing with this delphi stuff, so you need to explicitly create a TCellValue, where in delphi it would just be xls.SetCellValue(1,1,date); But sadly implicit operators in delphi don't map to implicit operators in c++ builder.