unit Unit1;
// Update: 15/08/2023.
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
uses
DateUtils,
fpSpreadsheet, fpstypes, fpsUtils,
xlsxooxml;
{$R *.lfm}
{ TForm1 }
//**************************************************
procedure TForm1.Button1Click(Sender: TObject);
const
HEADER_FONT_SIZE = 10; // in Points
DEFAULT_FONT_SIZE = 12; // in Points
NICE_ROWHEIGHT_FACTOR = 1.5; // adds extra margin to the cell
var
VarWORKBOOK: TsWorkbook;
VarWORKSHEET: TsWorksheet;
VarCELL: pCell;
VarR: Integer; // Row
VarC: Integer; // Column
begin
//---------------------------------------------------------
// Create a workbook
//---------------------------------------------------------
VarWORKBOOK := TsWorkbook.Create;
try
// Add a worksheet to the workbook
VarWORKSHEET := VarWORKBOOK.AddWorksheet('Export from grid');
// set default Font for the entire workbook //
// Later on the Font Size for the "header" will be changed !!! //
VarWORKBOOK.SetDefaultFont('Arial',DEFAULT_FONT_SIZE);
VarWORKSHEET.WriteDefaultRowHeight(DEFAULT_FONT_SIZE*NICE_ROWHEIGHT_FACTOR, suPoints);
//************************************************************
// set default Column widths like in the StrngGrid //
//Making use of "lpsUtils" in uses //
for VarC := 0 to StringGrid1.ColCount-1 do
VarWORKSHEET.writecolwidth
(VarC, pxToPts(StringGrid1.ColWidths[VarC],Screen.PixelsPerInch),suPoints);
//--------------------------------------------------------
// Write the title row
//---------------------------------------------------------
for VarC := 0 to StringGrid1.ColCount-1 do
begin
//Write the text of the header cell //
VarCELL:= VarWORKSHEET.WriteText(0, VarC, StringGrid1.Cells[VarC, 0]);
// Center text Horizontally //
VarWORKSHEET.WriteHorAlignment(VarCELL,haCenter);
// Center text Vertically //
VarWORKSHEET.WriteVertAlignment(VarCELL,vaCenter);
// Draw a default border around each cell
VarWORKSHEET.writeborders(VarCELL,[cbNorth,cbSouth,cbEast,cbWest]);
// text in header row in Bold //
VarWORKSHEET.WriteFontStyle(VarCELL, [fssBold]);
// text header font size //
VarWORKSHEET.WriteFontSize(VarCELL,HEADER_FONT_SIZE);
// name of the Font //
VarWORKSHEET.WriteFontName(VarCELL, 'Arial');
// Color Background of header cell //
VarWORKSHEET.WriteBackgroundColor(VarCELL,scYellow);
end;
// Set the row height for the header row which has a smaller font than
// the rest
VarWORKSHEET.WriteRowHeight(0, HEADER_FONT_SIZE*NICE_ROWHEIGHT_FACTOR, suPoints);
//-------------------------------------------------
// Write the data cells
//-------------------------------------------------
for VarR := 1 to StringGrid1.RowCount-1 do
begin
for VarC := 0 to StringGrid1.ColCount-1 do
begin
VarCELL:= VarWORKSHEET.WriteText
(VarR,VarC,StringGrid1.Cells[VarC,VarR]);
// write border around the cells //
VarWORKSHEET.WriteBorders
(VarCELL,[cbNorth,cbSouth,cbEast,cbWest]);
VarWORKSHEET.WriteHorAlignment
(VarCELL,haCenter);
// By default cells are bottom-aligned. It looks better if they are centered.
VarWORKSHEET.WriteVertAlignment
(VarCELL,vaCENTER);
end;
end;
(*
// Just to show you automatic row height adjustment (not needed since the
// row heights have been handled already).
//---------------------------------------------------------
// Adjust the row heights
//---------------------------------------------------------
for VarR := 0 to VarWORKSHEET.GetLastRowIndex do
begin
VarWORKSHEET.WriteRowHeight(VarR, NICE_ROWHEIGHT_FACTOR * VarWORKSHEET.CalcAutoRowHeight(VarR), VarWORKBOOK.Units);
end;
*)
//---------------------------------------------------------
// Save the workbook as xlsx file
//---------------------------------------------------------
Showmessage('Will save to xlsx');
VarWORKBOOK.WriteToFile('Test.xlsx', sfOOXML, true);
// workbook.WriteToFile('Test.ods', sfOpenDocument, true);
// or save it for LibreOffice
Showmessage('Saving done !');
finally
// Destroy the workbook after usage.
VarWORKBOOK.Free;
end;
end;
end.