Here's an initial version of a rather general exporter for all grid-like components and various data formats.
The exporter comes in two pieces:
- the Exporter itself which resembles something like the file format. I implemented CSV, HTML and spreadsheet formats (using fpspreadsheet). In addition to Bart's suggestions above it is very general, it does not even know the grid
- the ExporterLink which links the exporter to the object to be exported. In this way, any grid-like object can be exported, such as TCustomDrawGrid descendants or TKGrid or even TListView. For each one of them a descendant of TLazExporterLink must be provided which implements its abstract methods to iterate through the "grid" and to pass the cell values to the exporter.
Unit
LazExporter contains the abstract classes,
TLazExporter and
TLazExporterLink, as well as the
TCSVExporter (which creates a csv file/stream) and the
THTMLExporter (which creates a HTML file/stream). They offer their own set of properties to control the output. THTMLExporter, for example, has a stringlist "CSS" for formatting. TCSVExporter takes advantage of fpc's unit csvreadwrite, a part of the old CSVDocument (in memoriam of Rainier).
Unit
GridExporter implements the ExporterLink for TCustomDrawGrid descendents which is controlled by a set of parameters for enabling export of FixedCols, FixedRows, or VisibleColumnsOnly. Since TCustomDrawGrid does not expose string content I added a virtual method TCustomDrawGrid.GetCellText which queries the cell text by means of an event OnGetCellText; TStringGrid, of course, returns the Cells[col,row] here. I did this because "virtual grids" might be constructed from TCustomDrawGrid in such an event-driven way. For testing, you must replace your grids.pas unit by the one in the attachment (there is also a patch to see the changes).
Unit
SpreadsheetExporter implements the
TSpreadsheetExporter which writes to the spreadsheet formats provided by fpspreadsheet.
The attachment contains also a little demo to see things at work.
Please note that this work is not yet completed, I am posting it in this early stage to see if I am on the right way. Some control properties of the TCSVExporter provided by csvreadwrite are still missing. And I would also like to implement a basic TListviewExporterLink to demonstrate the interface to a non-Grid component.
Once finished, the GridExporter could be used to remove all hardcoded csv-writing from grids.pas (SaveToCSV, CopyToClipboard).