unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, fpstypes, fpspreadsheet, fpsallformats, fpsopendocument, xlsxooxml, fpsutils,
LCLIntf;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
public
procedure SaveFPS;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
const
loremipsum =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed feugiat efficitur consectetur. Duis maximus in nulla eget cursus. Nullam viverra orci ac lacus ' +
'euismod sagittis at eget purus. Vestibulum non magna massa. Vivamus nec arcu sed tortor pellentesque maximus. Suspendisse commodo, mauris non ullamcorper ' +
'consectetur, justo quam maximus ex, sit amet cursus nulla lorem vel ipsum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos ' +
'himenaeos. Praesent ultricies commodo odio et sollicitudin. Donec aliquet ligula ac orci iaculis tincidunt. Lorem ipsum dolor sit amet, consectetur adipiscing ' +
'elit.Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Donec pulvinar viverra dui a euismod. Class aptent taciti ' +
'sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed nibh tellus, viverra id dui quis, auctor congue sapien. ';
procedure TForm1.Button1Click(Sender: TObject);
begin
SaveFPS;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
end;
procedure TForm1.SaveFPS;
const
fnarr: Array[0..2] of String = ('Arial', 'Courier', 'TimesNewRoman');
fsarr: Array[0..3] of TsFontStyles = ([], [fssBold], [fssItalic], [fssBold, fssItalic]);
var
plik: TsWorkbook;
arkusz: TsWorksheet;
i, j: Integer;
sl: TStringList;
fnt: TFont;
function TsFontToTFont(afont: TsFont): TFont;
begin
Result := TFont.Create;
Result.Name := afont.FontName;
Result.Size := trunc(afont.Size);
Result.Style := [];
if fssBold in afont.Style then
Result.Style := Result.Style + [fsBold];
if fssItalic in afont.Style then
Result.Style := Result.Style + [fsItalic];
if fssUnderline in afont.Style then
Result.Style := Result.Style + [fsUnderline];
if fssStrikeOut in afont.Style then
Result.Style := Result.Style + [fsStrikeOut];
end;
begin
sl := TStringList.Create;
sl.StrictDelimiter := False;
sl.DelimitedText := loremipsum;
plik := TsWorkbook.Create;
arkusz := plik.AddWorksheet('test');
for j := 0 to 9 do
begin
if j = 0 then
arkusz.WriteColWidth(j, 50, suChars);
arkusz.WriteText(0, j, Format('Column %d', [j + 1]));
end;
arkusz.WriteText(0, 10, 'Col0 text width');
for i := 1 to 100 do
begin
for j := 0 to 4 do
begin
arkusz.WriteFont(i, j, fnarr[Random(Length(fnarr))], Random(10) + 6, fsarr[Random(Length(fsarr))], scBlack, fpNormal);
arkusz.WriteText(i, j, sl[Random(sl.Count)]);
end;
for j := 5 to 9 do
arkusz.WriteNumber(i, j, Random(50));
fnt := TsFontToTFont(arkusz.ReadCellFont(arkusz.GetCell(i, 0)));
Canvas.Font := fnt;
arkusz.WriteNumber(i, 10, Canvas.TextWidth(arkusz.ReadAsText(i, 0)));
fnt.Free;
end;
//save to file
plik.WriteToFile('testfile.xlsx', sfOOXML, True);
plik.Free;
OpenDocument('testfile.xlsx');
end;
end.