I have to save all different sheets in a xlsx file to csv by using FdxSpreadSheet. I want to pass the current index of sheets to the load from file and save them by adding the index at the end of the name. It doens't work and it always save the first sheet with different index name N times. How can I load different Sheets and save them in different files ?
FdxSpreadSheet := TdxSpreadSheet.Create(nil);
try
aFileText := ChangeFileExt(CurrentFile,'.csv');
FdxSpreadSheet.LoadFromFile(CurrentFile);
for I:=0 to FdxSpreadSheet.SheetCount -1 do begin
aFileText := ChangeFileExt(CurrentFile,IntToStr(I) '.csv');
FdxSpreadSheet.LoadFromFile(FdxSpreadSheet.Sheets[I]);
FdxSpreadSheet.SaveToFile(aFileText);
Result := aFileText;
LogModule.ScriviLog('File convterd from XlsX to CSV ',Format(':[%s]',[aFileText]),tpLiv4);
end;
finally
FreeAndNil(FdxSpreadSheet);
end;
CodePudding user response:
LoadFromFile() loads all the sheets if you load an xlsx file. Saving to CSV saves the currently active sheet so loop over the sheets.
for a := 0 to dxSpreadSheet1.SheetCount -1 do
begin
dxSpreadSheet1.ActiveSheetIndex := a;
dxSpreadSheet1.SaveToFile('Sheet_' intToStr(a 1) '.csv');
end;
Note: Tested with Delphi 11 and DeveloperExpress VCL ExpressSpreadSheet 21.2.2
