program project1;
uses
SysUtils, Classes, Graphics, LCLIntf, fpSpreadsheet, fpsTypes, fpsallformats;
var
wbook: TsWorkbook;
wsheet: TsWorksheet;
image1: TPortableNetworkGraphic;
image2: TPortableNetworkGraphic;
astream: TMemoryStream;
begin
wbook := TsWorkbook.create;
image1 := TPortableNetworkGraphic.Create;
image2 := TPortableNetworkGraphic.Create;
astream:= TMemoryStream.Create;
try
(* In the target application the image is created by the app and not loaded from a file. *)
image1.LoadFromFile('Image1.png');
image2.LoadFromFile('Image2.png');
(* Writing the image to the sheet directly from a file works for both .ods and .xlsx formats. *)
//wsheet := wbook.AddWorksheet('Image1');
//wsheet.WriteImage(0, 0, 'Image1.png');
//wsheet := wbook.AddWorksheet('Image2');
//wsheet.WriteImage(0, 0, 'Image2.png');
//wbook.WriteToFile('Test1_WriteImage(File).xlsx', true);
//wbook.WriteToFile('Test1_WriteImage(File).ods', true);
//wbook.clear;
(* Copying a spreadsheet containing the image from excel to calc format works. *)
//wbook.ReadFromFile('Test1_WriteImage(File).xlsx');
//wbook.WriteToFile('Test2_SpreadsheetCopy.ods', true);
(*
Writing the image to the sheet using a stream works on V1.16, but not on V2.00.
Attempting to open image_03.ods in Calc results in an error:
Format error discovered in the file in sub-document content.xml at 1,4466(row,col).
*)
wbook.Clear;
wsheet := wbook.AddWorksheet('Image 1');
image1.SaveToStream(astream);
wsheet.WriteImage(0, 0, astream, 1.0, 1.0);
wsheet := wbook.AddWorksheet('Image 2');
astream.Clear; // If this was forgotten, the application used to hang - now loads Image1 for Image2.
image2.SaveToStream(astream);
wsheet.WriteImage(0, 0, astream, 1.0, 1.0);
wbook.WriteToFile('Test3_WriteImage(Stream).xlsx', true);
wbook.WriteToFile('Test3_WriteImage_01(Stream).ods', true);
OpenDocument('Test3_WriteImage_01(Stream).ods');
finally
wbook.Free;
image1.Free;
image2.Free;
astream.Free;
end;
end.