Problem changing Excel-Version

Questions and answers on how to use XLSReadWriteII 5.
Post Reply
Emil1957
Posts: 4
Joined: Tue May 13, 2014 8:32 am

Problem changing Excel-Version

Post by Emil1957 »

Hello

we recently updated form XLSReadWriteII4 to XLSReadWriteII5 (5.20.22). I have a problem when the Excel-Version is modified during runtime. Our situation is as follows:

-User can export data from our software to an Excel file, the data are written to a XLSReadWriteII5 object
-The content of the XLSReadWriteII5 object ist then displayed in a preview window (using simple grid objects)
-In the preview window, the user can select the file format (Excel97 or Excel2007), the format is assigned just before calling "Write"
-If the selected format is different from the format set at creation time, the file is not correctly created

Example code

This works (Version is set directly after Create)

procedure TForm1.Button1Click(Sender: TObject);
var
ir, ic: integer;
sln, stxt: string;
ext: string;
xlsRW: TXLSReadWriteII5;
filename: string;
begin
try
xlsRW := TXLSReadWriteII5.Create( self );
// This works
xlsRW.Version := xvExcel97;

with xlsRW[0] do
begin
Name := 'Data export';
for ic := 0 to 5 do
for ir := 0 to 5 do
AsString[ ic-1, ir ] := IntToStr( ic*ir );
end;
if xlsRW.Version = xvExcel2007 then
filename := ExtractFilePath( Application.Exename ) + 'testxls.xlsx'
else
filename := ExtractFilePath( Application.Exename ) + 'testxls.xls';
xlsRW.Filename := filename;
xlsRW.Write;
finally
FreeAndNil( xlsRW );
end;

end;

This does not work (Version is set directly before Write)
The file is created (and can be openend with Excel2000, but nothing ist displayed)
procedure TForm1.Button1Click(Sender: TObject);
var
ir, ic: integer;
sln, stxt: string;
ext: string;
xlsRW: TXLSReadWriteII5;
filename: string;
begin
try
xlsRW := TXLSReadWriteII5.Create( self );
with xlsRW[0] do
begin
Name := 'Data export';
for ic := 0 to 5 do
for ir := 0 to 5 do
AsString[ ic-1, ir ] := IntToStr( ic*ir );
end;
// This does not work
xlsRW.Version := xvExcel97;

if xlsRW.Version = xvExcel2007 then
filename := ExtractFilePath( Application.Exename ) + 'testxls.xlsx'
else
filename := ExtractFilePath( Application.Exename ) + 'testxls.xls';
xlsRW.Filename := filename;
xlsRW.Write;
finally
FreeAndNil( xlsRW );
end;

end;
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: Problem changing Excel-Version

Post by larsa »

Hello

You must set excel version before you add any data.
Lars Arvidsson, Axolot Data
Emil1957
Posts: 4
Joined: Tue May 13, 2014 8:32 am

Re: Problem changing Excel-Version

Post by Emil1957 »

Thank you for the Information. Was that different in Verion 4?

I have another question: Hoe can I determine, if an Excel file ist in "Mac" format. IN Version 4 this coud be done by
xlsRW.Workbook.Date1904 (where xlsRW is an TXLSReadWriteII4 object)
In Version 5 I get a compiler error wehen I use this syntax
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: Problem changing Excel-Version

Post by larsa »

Hello

Yes, that was different in v4.

The Date1904 property is in XLS.OptionsDialog.Date1904
Lars Arvidsson, Axolot Data
Post Reply