DTO mapping
Single edition: the DTO layer (lxDto) ships in the core HotXLS package (HotXLSLaz.lpk for Lazarus), so it is included in Single, Small Business and Enterprise source and DCU editions, and the trial build ships it under the usual trial limits
lxDto writes the published properties of ordinary objects to a worksheet and reads them back, driven either by attributes declared on the class or by an explicit schema value
Declaring the columns
| [XlsColumn] | Title, Order, Format, Width and Hidden for one property’s column |
| [XlsMap] | reads the column using a different header text, which is the text written on export as well |
| [XlsIgnore] | leaves the property out of the mapping |
| [XlsRequired] | import rejects any row where that property's cell is empty, naming both the row and the header |
Attributes are honoured on the Delphi compiler. Elsewhere ({$IFDEF FPC}) each published property turns into a column named after it, and the explicit schema below conveys the same information
Schema
| TXlsDtoSchema | Add, Clear, IndexOf, HeaderOf, SortByOrder, Count and Columns hold the ordered mapping |
| TXlsDtoColumn | PropertyName, Title, Order, Format, Width, Hidden, Required and SourceName |
| TXlsDto | SchemaOf derives a schema from a class, Export writes objects, Import builds them back |
Export and import
TXlsDto.Export writes the header row at the start cell named by the caller, then one row per object, applying each column’s number format, width and hidden flag; a date property with no explicit format gets an ISO date format. TXlsDto.Import matches the schema headers to the sheet, creates one instance for every data row via the class reference, converts each cell to the property’s own type (integer, float, date, Boolean, enumeration or text), then appends the instances to the caller’s TObjectList; a row whose mapped cells are all empty ends the data
The class needs a parameterless constructor, declared virtual so that the class reference calls it; a TComponent descendant already owns one. A schema handed to either call is taken as it stands and sorted by Order; passing nil derives one from the class
Invalid mapping arguments or data raise EXlsDto: a start cell outside the sheet, a schema with no class or columns, a column with no property name or one past the property count, an export with no worksheet or object list, a nil object or a property the class does not publish, an import with no worksheet, class or list, a schema header the sheet does not carry, an empty cell marked required, or a cell that cannot be converted to the property’s type
Example
TCustomerDto = class(TPersistent)
published
[XlsColumn('Code', 1, '', 10)]
[XlsRequired]
property Code: string ...;
[XlsColumn('Customer', 2, '', 24)]
property Name: string ...;
[XlsColumn('Amount', 3, '#,##0.00', 12)]
property Amount: Double ...;
[XlsMap('E-mail')]
property Email: string ...;
[XlsIgnore]
property Internal: string ...;
end;
Schema := TXlsDto.SchemaOf(TCustomerDto);
try
TXlsDto.Export(Sheet, [TObject(A), TObject(B)], Schema);
Rows := TXlsDto.Import(Sheet, TCustomerDto, Schema, Items);
finally
Schema.Free;
end;
See also
Compiled reporting API binds an object graph to a template through TXLSReportObjectValues, which flattens the same kind of properties into named values instead of rows