HotXLS Docs

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; the trial build includes it under the usual trial limits

lxDto writes the published properties of ordinary objects into a worksheet and reads them back, driven either by attributes 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 from a different header text, which is also the text written on export
[XlsIgnore]leaves the property out of the mapping
[XlsRequired]import refuses a row whose cell for that property is empty, naming the row and the header

Attributes are honoured on the Delphi compiler. Elsewhere ({$IFDEF FPC}) every published property becomes a column of its own name, and the explicit schema below carries the same information

Schema

TXlsDtoSchemaAdd, Clear, IndexOf, HeaderOf, SortByOrder, Count and Columns hold the ordered mapping
TXlsDtoColumnPropertyName, Title, Order, Format, Width, Hidden, Required and SourceName
TXlsDtoSchemaOf 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 the caller names, then one row per object, and applies each column’s number format, width and hidden flag; a date property without an explicit format takes an ISO date format. TXlsDto.Import matches the schema headers against the sheet, creates one instance per data row through the class reference, converts each cell to the property’s own type (integer, float, date, Boolean, enumeration or text) and 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 has one. A schema passed to either call is used 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 without a class or columns, a column without a property name or past the property count, an export without a worksheet or object list, a nil object or a property the class does not publish, an import without a worksheet, class or list, a schema header the sheet does not carry, an empty cell marked required, or a cell that cannot convert 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