Blog

All Blog Posts  |  Next Post  |  Previous Post

Next Generation Data Grid for Delphi: Import & Export

Tuesday, November 12, 2024

TMS Software Delphi  Components

Intro

If you’re developing in Delphi and looking for a powerful, flexible, and highly customizable data grid solution, then TMS FNC Data Grid is the perfect choice. In this final blog, we continue on our journey with the TMS FNC Data Grid. The TMS FNC Data Grid offers developers advanced tools for presenting and interacting with data. A key feature is its support for importing and exporting data from and to various file formats. This functionality allows developers to perform summary operations such as sums, averages, counts, minimum, and maximum values directly within the grid, without the need for external code or complex calculations.


CSV

TMS FNC Data Grid makes it easy to import and export data to CSV format, a widely used and accessible data format. The methods to save & load are

procedure SaveToCSVData(AFileName: String; AEncoding: TEncoding);
procedure LoadFromCSVData(AFileName: String; AEncoding: TEncoding);

SaveToCSVData saves cell data to a CSV file, while LoadFromCSVData loads cell data from a CSV file.

Options

  • Grid.Options.IO.Delimiter: This specifies the delimiter to use when saving to and loading from CSV files. By default, the delimiter is set to #0, which allows for automatic delimiter detection when loading data from a CSV file. When saving to a CSV file, the ; character is used as the separator if the delimiter is set to #0. Setting the delimiter to a value other than #0 forces the CSV functions to use the specified delimiter exclusively.
  • Grid.Options.IO.QuoteEmptyCells: When set to true, an empty cell in the CSV file is saved as "". If false, no characters are written for empty cells.
  • Grid.Options.IO.AlwaysQuotes: When set to true, every cell value is saved with both prefix and suffix quotes. If false, quotes are only used when the cell data contains the delimiter character. Additionally, if the cell data includes quotes, they are written as doubled quotes in the file. By default, data is loaded into the grid starting from the first normal cell, which is typically cell 1,1 (considering there is one fixed row and one fixed column). To override this default behavior and specify a different starting cell for loading data, you can use the public properties:
    Grid.Options.IO.StartColumn := 0; 
    Grid.Options.IO.StartRow := 0;


JSON (Import Only)

TMS FNC Data Grid enables seamless data import from JSON, a popular, lightweight data-interchange format. With JSON support, developers can easily load structured data into grids, making it ideal for applications that require data sharing, web integration, and configuration management. 

procedure LoadFromJSONData(AFileName: string; ARowDataName: string = ''; AColumnDataName: string = ''; AColumnNames: TTMSFNCDataGridDataJSONNamesArray = nil); overload; virtual;

Parameters

  • AFileName: string: This is the primary parameter and represents the name (and potentially the path) of the JSON file to be loaded. The grid will read data from this file and populate the cells accordingly.
  • ARowDataName: string: (optional, default is an empty string) This parameter specifies the key in the JSON structure that contains the row data. By default, if this is left as an empty string, the procedure will try to auto-detect or use the default key for rows. Setting this allows you to target a specific section within the JSON for the row data, which is useful if the JSON has a nested or structured format.
  • AColumnDataName: string (optional, default is an empty string) Similar to ARowDataName, this parameter specifies the key for column data within the JSON. By explicitly setting this parameter, you can control where the grid looks for column data, which is useful for JSON files that have columns defined separately from rows or use a specific naming convention for columns.
  • AColumnNames: TTMSFNCDataGridDataJSONNamesArray (optional, default is nil) This parameter allows you to define an array of column names to be used when loading data. If the JSON data doesn’t explicitly define column names or if you want to rename columns as they are imported, you can pass an array of names here. This is helpful when you want to standardize column headers regardless of the JSON file’s original structure.


HTML (Export Only)

Exporting to HTML allows developers to transform grid content into a structured, styled table that can be viewed directly in web browsers or embedded into web pages. This feature is particularly useful for applications that require reporting, data presentation, or seamless integration with web-based environments.

procedure SaveToHTMLData(AFileName: String);

Options

The Options.IO.HTML property is designed to provide various configuration options for exporting to HTML format. By customizing these options, developers can control the appearance, formatting, and specific content included in the exported HTML. Below is an explanation of some properties and its role in HTML export:

  • ConvertSpecialChars: Boolean: Controls whether special characters are converted to their HTML-safe equivalents. For example, & becomes &. Default is False.
  • ExportColors: Boolean: Determines whether cell background and text colors from the grid should be included in the exported HTML. Default is True, which applies color styling to the HTML output.
  • ExportImages: Boolean: Controls whether images in the grid cells are exported to HTML. Default is True, enabling image inclusion.
  • ImageFolder: string: Specifies the folder path for saving images when the grid contains images. Setting this path ensures the images are saved correctly in an accessible location for the HTML document.
  • HeaderText string: Allows for custom HTML content or text at the beginning of the HTML file. It can include introductory or descriptive text, or even HTML tags for styling.
  • FooterText: string: Similar to HeaderText, this property allows adding custom HTML content or text at the end of the HTML file.
  • Summary: string: Adds a summary attribute to the HTML table, often used for accessibility purposes. This property provides a brief description of the table's contents.
  • TableStyle: string: Adds inline CSS styles to the HTML table, giving developers control over custom styling directly within the HTML.


PDF (Export Only)

To export to PDF, start by placing a TTMSFNCDataGridPDFIO component on the form and link the DataGrid property of this non-visual component to the desired TTMSFNCDataGrid instance.

To save the grid as a PDF, simply call
TMSFNCDataGridPDFIO.Save(FileName);
Alternatively, you can export to a stream by passing a TStream instance to the Save method.

The TTMSFNCDataGridPDFIO component offers configurable settings for margins, headers, footers, and PDF metadata, such as creator, author, title, and keywords. These options can be customized through the Options and Information properties.

TMS Software Delphi  Components

XLS

The TTMSFNCDataGridExcelIO component allows seamless reading from and writing to Excel .XLS files without requiring Excel to be installed.

To use TTMSFNCDataGridExcelIO for Importing and Exporting ..XLS Files:

  1. Place both the TTMSFNCDataGrid and TTMSFNCDataGridExcelIO components on your form.
  2. Link the DataGrid property of TTMSFNCDataGridExcelIO to the TTMSFNCDataGrid instance.
  3. (Optional) Configure TTMSFNCDataGridExcelIO properties to customize Excel file behavior. Default settings usually work well.
To import & export use one of the methods below

TMSFNCDataGridExcelIO.XLSImport(FileName);
TMSFNCDataGridExcelIO.XLSExport(FileName);

Properties

Below is a list of a couple of properties available when importing or exporting:

  • AutoResizeGrid: Boolean: This property, when set to True, automatically adjusts the grid's dimensions (number of columns and rows) to fit the imported Excel data.
  • GridStartCol: Integer, GridStartRow: Integer: These properties define the top-left starting point for import/export operations. They determine from which column and row in the Excel sheet the data will be read or written.
  • ExportCellFormats: Boolean: When set to True, it ensures that the original cell formats (such as string, integer, date, and float) are preserved during export, rather than converting all cells to strings.
  • ImportCellFormats: Boolean: This property, when True, imports the original formatting of cells from the Excel sheet, ensuring that styles such as fonts and colors are retained.
  • ExportImages: Boolean: When set to True, this property ensures that any images within the grid are also exported to the Excel file, preserving the visual content along with the data.
  • ImportClearCells: Boolean: If set to True, this property will clear all existing cells in the grid before importing new data from the Excel file. This ensures that the grid is emptied and refreshed with the imported content.


Custom Column-based Text

The TMS FNC Data Grid provides functionality to save and load data using a proprietary file format in readable text with a specified encoding. The methods to save & load are

procedure SaveToFileData(AFileName: String; AEncoding: TEncoding);
procedure LoadFromFileData(AFileName: String; AEncoding: TEncoding);

SaveToFileData saves cell data and column widths, while LoadFromFileData loads cell data and column widths from the same format.

Conclusion

TMS FNC Data Grid provides powerful import and export features for handling grid data across multiple formats, including CSV, Excel, HTML, PDF and JSON. These capabilities make it easy to populate grids with external data and export processed data for reporting or sharing. By simplifying data management, TMS FNC Data Grid enhances the versatility and efficiency of FNC-based applications.



Pieter Scheldeman


  1. Next Generation Data Grid for Delphi: Getting Started

  2. Next Generation Data Grid for Delphi: Adding, Formatting & Converting Data

  3. Next Generation Data Grid for Delphi: Filtering & Sorting

  4. Next Generation Data Grid for Delphi: Grouping

  5. Next Generation Data Grid for Delphi: Webinar Replay Available!

  6. Next Generation Data Grid for Delphi: Cell Controls

  7. Next Generation Data Grid for Delphi: Master-Detail

  8. Next Generation Data Grid for Delphi: Calculations

  9. Next Generation Data Grid for Delphi: Import & Export



This blog post has not received any comments yet.



Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post