fpspreadsheet的强大之处在于,可以完全不必依赖外部应用程序,如excel和wps,快速读写电子表格数据和格式。如果使用com方式调用外部应用程,速度太慢了,而且受到不少限制。
这里简单演示生成xlsx文件,并且设置部分单元格边框属性和底色。
向部分单元格写入文本并设置边框属性。
MyWorksheet.WriteText(1, 3, '[N]'); // D2
MyCell := MyWorksheet.GetCell(1, 3);
MyCell^.Border := [cbNorth];
MyWorksheet.WriteText(2, 1, 'Compras');
MyCell := MyWorksheet.GetCell(2, 1);
MyCell^.Border := [cbWest];
MyCell^.BackgroundColor := scGrey10pct;
最终生成2个sheet。
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
xlsbiff8, fpsopendocument,
fpscell,
fpsTypes, fpspreadsheet, xlsxml, fpsutils, fpsallformats, fpsDataset;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
MyWorkbook: TsWorkbook;
MyWorksheet: TsWorksheet;
MyDir: string;
MyCell: PCell;
procedure WriteFirstWorksheet();
procedure WriteSecondWorksheet();
public
end;
var
Form1: TForm1;
implementation
{$R *.frm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
MyDir := ExtractFilePath(ParamStr(0));
// Create the spreadsheet
MyWorkbook := TsWorkbook.Create;
WriteFirstWorksheet();
WriteSecondWorksheet();
// Save the spreadsheet to a file
MyWorkbook.WriteToFile(MyDir + 'color.xlsx', sfExcel8, true);
MyWorkbook.Free;
showmessage('ok');
end;
procedure TForm1.WriteFirstWorksheet();
begin
MyWorksheet := MyWorkbook.AddWorksheet('Worksheet1');
// Write some cells
MyWorksheet.WriteText(1, 0, 'Border'); // A2
MyWorksheet.WriteText(1, 1, '[]'); // B2
MyCell := MyWorksheet.GetCell(1, 1);
MyCell^.Border := [];
MyWorksheet.WriteText(1, 3, '[N]'); // D2
MyCell := MyWorksheet.GetCell(1, 3);
MyCell^.Border := [cbNorth];
MyWorksheet.WriteText(1, 5, '[W]'); // F2
MyCell := MyWorksheet.GetCell(1, 5);
MyCell^.Border := [cbWest];
MyWorksheet.WriteText(1, 7, '[E]'); // H2
MyCell := MyWorksheet.GetCell(1, 7);
MyCell^.Border := [cbEast];
MyWorksheet.WriteText(1, 9, '[S]'); // J2
MyCell := MyWorksheet.GetCell(1, 9);
MyCell^.Border := [cbSouth];
MyWorksheet.WriteText(3, 1, '[N,W]'); // B4
MyCell := MyWorksheet.GetCell(3, 1);
MyCell^.Border := [cbNorth, cbWest];
MyWorksheet.WriteText(3, 3, '[N,E]'); // D4
MyCell := MyWorksheet.GetCell(3, 3);
MyCell^.Border := [cbNorth, cbEast];
MyWorksheet.WriteText(3, 5, '[N,S]'); // F4
MyCell := MyWorksheet.GetCell(3, 5);
MyCell^.Border := [cbNorth, cbSouth];
MyWorksheet.WriteText(3, 7, '[W,E]'); // H4
MyCell := MyWorksheet.GetCell(3, 7);
MyCell^.Border := [cbWest, cbEast];
MyWorksheet.WriteText(3, 9, '[W,S]'); // J4
MyCell := MyWorksheet.GetCell(3, 9);
MyCell^.Border := [cbWest, cbSouth];
MyWorksheet.WriteText(3, 11, '[E,S]'); // L4
MyCell := MyWorksheet.GetCell(3, 11);
MyCell^.Border := [cbEast, cbSouth];
MyWorksheet.WriteText(5, 1, '[N,W,E]');// B6
MyCell := MyWorksheet.GetCell(5, 1);
MyCell^.Border := [cbNorth, cbWest, cbEast];
MyWorksheet.WriteText(5, 3, '[N,W,S]');// D6
MyCell := MyWorksheet.GetCell(5, 3);
MyCell^.Border := [cbNorth, cbWest, cbSouth];
MyWorksheet.WriteText(5, 5, '[N,E,S]');// F6
MyCell := MyWorksheet.GetCell(5, 5);
MyCell^.Border := [cbNorth, cbEast, cbSouth];
MyWorksheet.WriteText(5, 7, '[W,E,S]');// H6
MyCell := MyWorksheet.GetCell(5, 7);
MyCell^.Border := [cbWest, cbEast, cbSouth];
MyWorksheet.WriteText(5, 9, '[N,W,E,S]');// J6
MyCell := MyWorksheet.GetCell(5, 9);
MyCell^.Border := [cbNorth, cbWest, cbEast, cbSouth];
MyCell^.BackgroundColor := scGreen;
end;
procedure TForm1.WriteSecondWorksheet();
begin
MyWorksheet := MyWorkbook.AddWorksheet('Worksheet2');
// Write some cells
// Line 1
MyWorksheet.WriteText(1, 1, 'Relatório');
MyCell := MyWorksheet.GetCell(1, 1);
MyCell^.Border := [cbNorth, cbWest, cbSouth];
MyCell^.BackgroundColor := scGrey20pct;
MyWorksheet.WriteText(1, 2, ' ');
MyCell := MyWorksheet.GetCell(1, 2);
MyCell^.Border := [cbNorth, cbEast, cbSouth];
MyCell^.BackgroundColor := scGrey20pct;
// Line 2
MyWorksheet.WriteText(2, 1, 'Compras');
MyCell := MyWorksheet.GetCell(2, 1);
MyCell^.Border := [cbWest];
MyCell^.BackgroundColor := scGrey10pct;
MyWorksheet.WriteText(2, 2, 'R$ 20');
MyCell := MyWorksheet.GetCell(2, 2);
MyCell^.Border := [cbEast];
MyCell^.BackgroundColor := scGrey10pct;
// Line 3
MyWorksheet.WriteText(3, 1, 'Total:');
MyCell := MyWorksheet.GetCell(3, 1);
MyCell^.Border := [cbWest, cbSouth];
MyCell^.BackgroundColor := scGrey10pct;
MyWorksheet.WriteText(3, 2, 'R$ 20');
MyCell := MyWorksheet.GetCell(3, 2);
MyCell^.Border := [cbEast, cbSouth];
MyCell^.BackgroundColor := scGrey10pct;
end;
end.