FPSpreadsheet/fr
│
Deutsch (de) │
English (en) │
español (es) │
français (fr) │
polski (pl) │
русский (ru) │
La bibliothèque fpSpreadsheet offre un moyen pratique pour générer et lire des documents avec feuille de calcul sous différents formats. La bibliothèque est écrite de manière très flexible, susceptible d'être étendu pour supporter de nombreux formats facilement.
Documentation de l'API
Référence de l'API
Au format CHM ici:
http://lazarus-ccr.svn.sourceforge.net/viewvc/lazarus-ccr/components/fpspreadsheet/fpspreadsheet.chm
Formules
Deux types de formules sont pris en charge par FPSpreadsheet: Les formules type chaînes de caractères et les formules RPN. Les formules type chaînes de caractères sont écrites dans les chaînes comme dans l'application de bureau, par exemple "ROUND(A1+B1)",tandis que les formules RPN sont écrites en notation polonaise inversées (RPN), par exemple: A1, B1, Add, 0, ROUND
Formules RPN
Exemple d'utilisation de routines incluses
<delphi>
// Write the formula E1 = ABS(A1) SetLength(MyRPNFormula, 2); MyRPNFormula[0].ElementKind := fekCell; MyRPNFormula[0].Col := 0; MyRPNFormula[0].Row := 0; MyRPNFormula[1].ElementKind := fekABS; MyWorksheet.WriteRPNFormula(0, 4, MyRPNFormula);
// Write the formula F1 = ROUND(A1, 0) SetLength(MyRPNFormula, 3); MyRPNFormula[0].ElementKind := fekCell; MyRPNFormula[0].Col := 0; MyRPNFormula[0].Row := 0; MyRPNFormula[1].ElementKind := fekNum; MyRPNFormula[1].DoubleValue := 0.0; MyRPNFormula[2].ElementKind := fekROUND; MyWorksheet.WriteRPNFormula(0, 5, MyRPNFormula);
</delphi>
routines incluses
ABS - Retoune la valeur absolue d'un nombre donné. Prend 1 argument. Exemple: ABS(-1) = 1
ROUND - Arrondit un nombre à un nombre donné de décimales. Prend 2 arguments, d'abord le nombre à arrondir en ensuite le nombre de décimales avec lesquelles on réalise l'arrondi. Ce deuxième argument est zéro par défaut et il peut être négatif.
Les paquets
FPSpreadsheet est livré avec les paquets suivants pour faciliter son utilisation:
laz_fpspreadsheet.lpk
Ajoute tous les fichiers nécessaires à l'utilisation de fpspreadsheet dans vos projets Lazarus. After adding this package as a requirement to your project you can add "fpspreadsheet" to the uses clause of the units which use it.
laz_fpspreadsheet_visual.lpk
Adds a set of visual components which expands the capabilities of FPSpreadsheet to also include editing and displaying data on screen inside LCL applications.
The following LCL components are installed with this package:
Examples
To create a project which uses the fpspreadsheet library, add the fpspreadsheet_pkg package to it's Lazarus project, or add the base directory of fpspreadsheet to you compiler options if using another IDE.
Excel 5 example
<delphi> { excel5demo.dpr
Demonstrates how to write an Excel 5.x file using the fpspreadsheet library
You can change the output format by changing the OUTPUT_FORMAT constant
AUTHORS: Felipe Monteiro de Carvalho } program excel5demo;
{$mode delphi}{$H+}
uses
Classes, SysUtils, fpspreadsheet, fpsallformats, fpspreadsheet_pkg;
const OUTPUT_FORMAT = sfExcel5;
var
MyWorkbook: TsWorkbook; MyWorksheet: TsWorksheet; MyFormula: TsRPNFormula; MyDir: string;
begin
// Initialization MyDir := ExtractFilePath(ParamStr(0));
// Create the spreadsheet MyWorkbook := TsWorkbook.Create; MyWorksheet := MyWorkbook.AddWorksheet('My Worksheet');
// Write some number cells MyWorksheet.WriteNumber(0, 0, 1.0); MyWorksheet.WriteNumber(0, 1, 2.0); MyWorksheet.WriteNumber(0, 2, 3.0); MyWorksheet.WriteNumber(0, 3, 4.0);
// Write the formula E1 = A1 + B1 // or, in RPN: A1, B1, + SetLength(MyFormula, 3); MyFormula[0].ElementKind:=fekCell; {A1} MyFormula[0].Col := 0; MyFormula[0].Row := 0; MyFormula[1].ElementKind:=fekCell; {B1} MyFormula[1].Col := 1; MyFormula[1].Row := 0; MyFormula[2].ElementKind:=fekAdd;; {+} MyWorksheet.WriteRPNFormula(0, 4, MyFormula);
// Creates a new worksheet MyWorksheet := MyWorkbook.AddWorksheet('My Worksheet 2');
// Write some string cells MyWorksheet.WriteUTF8Text(0, 0, 'First'); MyWorksheet.WriteUTF8Text(0, 1, 'Second'); MyWorksheet.WriteUTF8Text(0, 2, 'Third'); MyWorksheet.WriteUTF8Text(0, 3, 'Fourth');
// Save the spreadsheet to a file MyWorkbook.WriteToFile(MyDir + 'test' + STR_EXCEL_EXTENSION, OUTPUT_FORMAT); MyWorkbook.Free;
end. </delphi>
Iterating through all Worksheets
<delphi> var
MyWorkbook: TsWorkbook; MyWorksheet: TsWorksheet; i: Integer;
begin
// Here load MyWorkbook from a file or build it for i := 0 to MyWorkbook.GetWorksheetCount() - 1 do begin MyWorksheet := MyWorkbook.GetWorksheetByIndex(i); // Do something with MyWorksheet end;
</delphi>
Converting a database to a spreadsheet
Note that this is only meta-code, not tested.
<delphi> program db5xls;
{$mode delphi}{$H+}
uses
Classes, SysUtils, // add database units fpspreadsheet, fpsallformats, fpspreadsheet_pkg;
const OUTPUT_FORMAT = sfExcel5;
var
MyWorkbook: TsWorkbook; MyWorksheet: TsWorksheet; MyDatabase: TSdfDataset; MyDir: string; i, j: Integer;
begin
// Initialization MyDir := ExtractFilePath(ParamStr(0)); // Open the database MyDatabase := TSdfDataset.Create; MyDatabase.Filename := 'test.dat'; // Add table description here MyDatabase.Active := True;
// Create the spreadsheet MyWorkbook := TsWorkbook.Create; MyWorksheet := MyWorkbook.AddWorksheet('My Worksheet'); // Write the field names for i := 0 to MyDatabase.Fields.Count - 1 do MyWorksheet.WriteUTF8Text(0, i, MyDatabase.Field[i].FieldName);
// Write all cells to the worksheet MyDatabase.First; j := 0; while not MyDatabase.EOF do begin for i := 0 to MyDatabase.Fields.Count - 1 do MyWorksheet.WriteUTF8Text(j + 1, i, MyDatabase.Field[i].AsString);
MyDatabase.Next; Inc(j); end;
// Close the database MyDatabase.Active := False; MyDatabase.Free;
// Save the spreadsheet to a file MyWorkbook.WriteToFile(MyDir + 'test' + STR_EXCEL_EXTENSION, OUTPUT_FORMAT); MyWorkbook.Free;
end. </delphi>
Converting between two spreadsheet formats
Note that this is only meta-code, not tested.
<delphi> program ods2xls;
{$mode delphi}{$H+}
uses
Classes, SysUtils, fpspreadsheet, fpsallformats, fpspreadsheet_pkg;
const
INPUT_FORMAT = sfOpenDocument; OUTPUT_FORMAT = sfExcel8;
var
MyWorkbook: TsWorkbook; MyDir: string;
begin
// Initialization MyDir := ExtractFilePath(ParamStr(0)); // Convert the spreadsheet MyWorkbook := TsWorkbook.Create; try MyWorkbook.ReadFromFile(MyDir + 'test.ods', INPUT_FORMAT); MyWorkbook.WriteToFile(MyDir + 'test.xls', OUTPUT_FORMAT); finally MyWorkbook.Free; end;
end. </delphi>
Download
Subversion
You can download FPSpreadsheet using the subversion software and the following command line:
svn co https://lazarus-ccr.svn.sourceforge.net/svnroot/lazarus-ccr/components/fpspreadsheet fpspreadsheet
Current Progress
Progress by supported format:
Format | Supports multiple sheets? | Supports Unicode? | Reader Progress | Writer Progress | Text | Number | String Formula | RPN Formula |
---|---|---|---|---|---|---|---|---|
Excel 2.x | No | No** | Working | Working | Working | Working | Not implemented | Working |
Excel 3.0 | No | No** | Not implemented | Not implemented | Not implemented | Not implemented | Not implemented | Not implemented |
Excel 4.0 | No | No** | Not implemented | Not implemented | Not implemented | Not implemented | Not implemented | Not implemented |
Excel 5.0 (Excel 5.0 and 95) | Yes | No** | Working* | Working | Working | Working | Not implemented | Working |
Excel 8.0 (Excel 97, 2000, XP and 2003) | Yes | Yes | Working* | Working | Working | Working | Not implemented | Not implemented |
Microsoft OOXML | Yes | Yes | Not implemented | Not implemented | Not implemented | Not implemented | Not implemented | Not implemented |
OpenDocument | Yes | Yes | Working | Working | Working | Working | Not implemented | Not implemented |
(*) Some cell could be returned blank due missing or non ready implemented number and text formats. (**) In formats which don't support unicode the data is stored as ISO 8859-1. Note that FPSpreadsheet offers UTF-8 read and write routines, but the data will be converted to ISO when reading or writing to the disk. Be careful that characters which don't fit ISO Latin 1 will be lost in those operations.
Changelog
Jan 2009
- Implemented a cross-platform support for OLE file. Now Excel 5.0 files can be created in any operating system.
- Adds read support for Excel 2.1
Feb 2008
- Initial commit to lazarus-ccr with write support for Excel 2.1, Excel 5.0 (Windows only) and experimental OOXML and OpenDocument
License
LGPL with static linking exception. This is the same license as is used in the Lazarus Component Library.
See also
External Links
- Microsoft OLE Document Format - http://sc.openoffice.org/compdocfileformat.pdf
- Excel file format description - http://sc.openoffice.org/excelfileformat.pdf