FPSpreadsheet
From Lazarus wiki
Revision as of 12:20, 22 January 2008 by Sekelsenmat (talk | contribs) (New page: Because of it's very simple format, it's easy to generate Excel 2.1 files from code. New Excel versions are still capable of reading those files, so this can be enough to automate the gene...)
Because of it's very simple format, it's easy to generate Excel 2.1 files from code. New Excel versions are still capable of reading those files, so this can be enough to automate the generation of simple Excel files. Unfortunately Excel 4 and older formats don't support multiple sheets in the same file, so this isn't supported by this method.
Here is a unit which can write Excel 2.1 files:
{ excel2utils.pas Writes an Excel 2.1 file To write the file, always start calling StartExcel2File to create the Excel file header, then call WriteExcel2Cell once for each cell value and finally call EndExcel2File to write the end of the file. License: The same modifyed LGPL as the Lazarus Component Library AUTHORS: Felipe Monteiro de Carvalho } unit excel2utils; {$ifdef fpc} {$mode delphi}{$H+} {$endif} interface uses Classes, SysUtils; const STR_EXCEL_EXTENSION = '.xls'; procedure WriteExcel2Cell(AStream: TStream; const ACol, ARow: Word; const AValue: string); procedure StartExcel2File(AStream: TStream); procedure EndExcel2File(AStream: TStream); implementation procedure WriteExcel2Cell(AStream: TStream; const ACol, ARow: Word; const AValue: string); var L: Word; CXlsLabel: array[0..5] of Word = ($204, 0, 0, 0, 0, 0); begin L := Length(AValue); CXlsLabel[1] := 8 + L; CXlsLabel[2] := ARow; CXlsLabel[3] := ACol; CXlsLabel[5] := L; AStream.WriteBuffer(CXlsLabel, SizeOf(CXlsLabel)); AStream.WriteBuffer(Pointer(AValue)^, L); end; procedure StartExcel2File(AStream: TStream); var CXlsBof: array[0..5] of Word = ($809, 8, 0, $10, 0, 0); begin AStream.WriteBuffer(CXlsBof, SizeOf(CXlsBof)); end; procedure EndExcel2File(AStream: TStream); var CXlsEof: array[0..1] of Word = ($0A, 00); begin AStream.WriteBuffer(CXlsEof, SizeOf(CXlsEof)); end; end.
And here is an example of how to use it:
{ excel2demo.dpr Demonstrates how to write an Excel 2.1 file using the excel2utils unit Public Domain AUTHORS: Felipe Monteiro de Carvalho } program excel2demo; {$mode delphi}{$H+} uses Classes, SysUtils; var OutputFile: TFileStream; MyDir: string; begin // Open the output file MyDir := ExtractFilePath(ParamStr(0)); OutputFile := TFileStream.Create(MyDir + 'test' + STR_EXCEL_EXTENSION, fmCreate or fmOpenWrite); StartExcel2File(OutputFile); // Write some cells WriteExcel2Cell(OutputFile, 0, 0, '1'); WriteExcel2Cell(OutputFile, 1, 0, '2'); WriteExcel2Cell(OutputFile, 2, 0, '3'); WriteExcel2Cell(OutputFile, 3, 0, '4'); // Close the files EndExcel2File(OutputFile); OutputFile.Free; end.