unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids,
ExtCtrls, StdCtrls, Buttons,
fpSpreadsheet, fpsTypes, xlsxOOXML, DateUtils,
[b] Process;[/b]
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Panel1: TPanel;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
function MyStrToFloat(s: String): Double;
var
fs: TFormatSettings;
begin
fs := FormatSettings;
fs.DecimalSeparator := '.';
if not TryStrToFloat(s, Result, fs) then
begin
fs.DecimalSeparator := ',';
Result := StrToFloat(s, fs);
end;
end;
function MyStrToDate(s: String): TDate;
begin
Result := ScanDateTime('yyyy"/"mm"/"dd', s);
end;
function MyStrToCurr(s: String): Double;
begin
Result := MyStrToFloat(Copy(s, 1, Length(s)-2));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
workbook: TsWorkbook;
worksheet: TsWorksheet;
r, c: Integer;
begin
// Create a workbook
workbook := TsWorkbook.Create;
try
// Add a worksheet to the workbook
worksheet := workbook.AddWorksheet('Export from grid');
// Write the title row
for c := 0 to StringGrid1.ColCount-1 do
worksheet.WriteText(0, c, StringGrid1.Cells[c, 0]);
// Write the data cells
for r := 1 to StringGrid1.RowCount-1 do
begin
for c := 0 to StringGrid1.ColCount-1 do
case c of
0: worksheet.WriteNumber(r, c,
StrToInt(StringGrid1.cells[c, r]), nfFixed, 0);
1: worksheet.WriteNumber(r, c,
MyStrToFloat(StringGrid1.Cells[c, r]), nfGeneral);
2: worksheet.WriteText(r, c,
StringGrid1.Cells[c, r]);
3: worksheet.WriteDateTime(r, c,
MyStrToDate(StringGrid1.Cells[c, r]), nfShortDate);
4: worksheet.WriteCurrency(r, c,
MyStrToCurr(StringGrid1.Cells[c, r]), nfCurrency, 2, '$');
end;
end;
// Save the workbook as xlsx file
workbook.SetDefaultFont('Arial',13);
// worksheet.DefaultRowHeight:= 1.3; => depricated (no longer in use)
worksheet.WriteDefaultRowHeight(1.3, suLines);
//Worksheet.WriteDefaultColWidth(10, suLines); not needed !!!!
//****************************************
Worksheet.WriteHorAlignment(0,0,haCenter);
Worksheet.WriteHorAlignment(0,1,haCenter);
Worksheet.WriteHorAlignment(0,2,haCenter);
Worksheet.WriteHorAlignment(0,3,haCenter);
Worksheet.WriteHorAlignment(0,4,haCenter);
//****************************************
Worksheet.WriteVertAlignment(0,0,vaCenter);
Worksheet.WriteVertAlignment(0,1,vaCenter);
Worksheet.WriteVertAlignment(0,2,vaCenter);
Worksheet.WriteVertAlignment(0,3,vaCenter);
Worksheet.WriteVertAlignment(0,4,vaCenter);
//****************************************
for r := 0 to StringGrid1.RowCount -1 do
worksheet.WriteHorAlignment(r, 1, haCenter);
for r := 0 to StringGrid1.RowCount -1 do
worksheet.WriteHorAlignment(r, 2, haCenter);
Worksheet.PageLayout.Options := Worksheet.PageLayout.Options + [poFitPages];
Worksheet.PageLayout.FitHeightToPages := 0; // use as many pages as needed
//****************************************
for r := 0 to StringGrid1.RowCount -1 do
Worksheet.WriteBorders(r, 0, [cbEast,cbWest,cbNorth,cbSouth]); // A1: row 0, column 0
for r := 0 to StringGrid1.RowCount -1 do
Worksheet.WriteBorders(r, 1, [cbEast,cbWest,cbNorth, cbSouth]); // B1: row 0, column 1
for r := 0 to StringGrid1.RowCount -1 do
Worksheet.WriteBorders(r, 2, [cbEast,cbWest,cbNorth, cbSouth]); //
for r := 0 to StringGrid1.RowCount -1 do
Worksheet.WriteBorders(r, 3, [cbEast,cbWest,cbNorth, cbSouth]); //
for r := 0 to StringGrid1.RowCount -1 do
Worksheet.WriteBorders(r, 4, [cbEast,cbWest,cbNorth, cbSouth]); //
//****************************************
workbook.WriteToFile('Test.xlsx', sfOOXML, true);
// this (sfOOXML)is XLSX format.
[b] RunCommand('C:\Windows\system32\calc.exe', ['Test.xlsx'], outputStr);[/b]
// requires unit "process" in "uses".
finally
// Destroy the workbook after usage.
workbook.Free;
end;
end;
end.