I am in the process of evaluating XLSReadWriteII 5 and have run into the following issue:
Consider the code below:
Code: Select all
TestDataset := TClientDataset.Create(nil);
TestDataset.FieldDefs.Add('Name', ftString, 20);
TestDataset.FieldDefs.Add('Address', ftString, 100);
TestDataset.FieldDefs.Add('Balance', ftCurrency);
TestDataset.FieldDefs.Add('DOB', ftDate);
TestDataset.CreateDataSet;
TestDataset.AppendRecord(['John Smith', '123 Fake Street', 244.50, IncMonth(Date, -200)]);
TestDataset.AppendRecord(['Elva May Ace', '244 Elms Street', 2000, IncMonth(Date, -50)]);
TestDataset.AppendRecord(['John Jenkinson', '55 Grace Avenue', 100, IncMonth(Date, -25)]);
Sheet1 := XLS.Sheets[0];
XLSDBRead := TXLSDBRead5.Create(nil);
XLSDBRead.Dataset := TestDataset;
XLSDBRead.IncludeFieldnames := True;
XLSDBRead.Sheet := XLS.Sheets[0].Index;
XLSDBRead.XLS := XLS;
XLSDBRead.Read;
for Index := 0 to TestDataset.FieldCount - 1 do begin
case TestDataset.FieldDefList.FieldDefs[Index].DataType of
ftDateTime, ftDate: begin
Sheet1.Columns[Index].NumberFormat := 'DD-MMM-YYYY';
end;
ftCurrency: begin
Sheet1.Columns[Index].NumberFormat := '[$£-809]#,##0.00;-[$£-809]#,##0.00';
Sheet1.Columns[Index].HorizAlignment := chaRight;
end;
end;
end;
Sheet1.Name := 'Contributions to Pension';
XLS.FileName := 'C:\TestFilename.xlsx';
XLS.Write;
Interestingly enough, if I don't use XLSDBRead to read a dataset and write it to a spreadsheet, but just do something like this:
Code: Select all
XLS.Add;
XLS.Sheets[1].AsDateTime[0, 1] := Date();
XLS.Sheets[1].Cell[0, 1].NumberFormat := 'DD-MMM-YYYY';
XLS.Sheets[1].AsFloat[0, 2] := 1234567.87;
XLS.Sheets[1].Cell[0, 2].NumberFormat := '[$£-809]#,##0.00;-[$£-809]#,##0.00';
XLS.Sheets[1].AutoWidthCols(0, 1);
XLS.FileName := 'C:\TestFilename2.xlsx';
XLS.Write;
Thanks for your help.
Cheers,
Kaye.