Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
News

Enterprise Connector Spotlight: Excel Files #ConnectTheData

 For June our Enterprise Connector Spotlight looks at Excel Files and Slack. For this article we are going to take a look at working with Excel Files. Register now to join the webinar and get your free Enterprise Connector licenses (free license for update subscription customers only).

Connecting to Excel

Many of the Enterprise Connectors support connecting to remote data sources hosted in the cloud via software as a service provides. The Excel File Enterprise Connector is different in that it supports connecting to a local Excel spreadsheet file. In theory the file could be stored on the network, but the practice is the same: It is accessing an Excel file via the filesystem.

When you define the connection you indicate the Excel file that the connector will access. One table is defined for each sheet within that file, and each column of the sheet becomes a column in the table.

You can change the orientation of the data in the sheets too. Here is the full list of connection parameters from the documentation:

Allow Formula Whether or not to treat values starting with an equals (=) sign as formulas during inserts and updates.
Auto Cache Automatically caches the results of SELECT queries into a cache database specified by either CacheLocation or both of CacheConnection and CacheProvider .
Buffer Changes Indicates whether to hold changes to the data in memory until the connection is closed.
Cache Connection The connection string for the cache database. This property is always used in conjunction with CacheProvider . Setting both properties will override the value set for CacheLocation for caching data.
Cache Location Specifies the path to the cache when caching to a file.
Cache Metadata This property determines whether or not to cache the table metadata to a file store.
Cache Provider The name of the provider to be used to cache data.
Cache Tolerance The tolerance for stale data in the cache specified in seconds when using AutoCache.
Define Tables Map Excel ranges to table names.
Empty Value Mode Indicates whether to read the empty values as empty or as null.
Excel File The location of an Excel file.
Has Cross Sheet References Indicates how cross sheet references are handled.
Header Indicates whether the first row should be used as a column header.
Ignore Calc Error Indicates whether to ignore errors that occurred during the calculation.
Location A path to the directory that contains the schema files defining tables, views, and stored procedures.
Logfile A path to the log file.
Maximum Column Size The maximum column size.
Max Log File Size A string specifying the maximum size in bytes for a log file (for example, 10 MB). When the limit is hit, a new log is created in the same folder with the date and time appended to the end.
Max Rows Limits the number of rows returned rows when no aggregation or group by is used in the query. This helps avoid performance issues at design time.
Null Value Mode Indicates whether to read empty cells as null or as empty.
Offline Use offline mode to get the data from the cache instead of the live source.
Orientation Indicates whether the data in Excel is laid out horizontally or vertically.
Other These hidden properties are used only in specific use cases.
Pseudo Columns This property indicates whether or not to include pseudo columns as columns to the table.
Readonly You can use this property to enforce read-only access to Excel from the provider.
Recalculate Indicates whether to recalculate all formulas when data is read.
Row Scan Depth Set this property to control the number of rows scanned when TypeDetectionScheme is set to RowScan.
RTK The runtime key used for licensing.
Show Empty Rows Indicates whether or not the empty rows should be pushed.
Support Enhanced SQL This property enhances SQL functionality beyond what can be supported through the API directly, by enabling in-memory client-side processing.
Tables This property restricts the tables reported to a subset of the available tables. For example, Tables=TableA,TableB,TableC.
Type Detection Scheme Determines how the provider detects the data types of columns.
Verbosity The verbosity level that determines the amount of detail included in the log file.
Views Restricts the views reported to a subset of the available tables. For example, Views=ViewA,ViewB,ViewC.

Reading and Writing Data

Once you are connected you can use the TFDConnection’s GetTableNames and GetFieldNames to enumerate the sheets of the spreadsheet and see what columns are defined.

Fullscreen

1
2
3
4
FDConnection1.Connected := True;
FDConnection1.GetTableNames(‘cdata’,’excel’,”,listbox1.Items);
ListBox1.ItemIndex := 0;
FDConnection1.GetFieldNames(‘cdata’,’excel’,listbox1.Items[0],”,listbox2.Items);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

From there you simply write standard SQL to read and write date from the spreadsheet just like it was a database. 

Fullscreen

1
SELECT RowId, Name, Item, Quantity, Amount FROM Sheet1 WHERE Amount > ’50’
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

There also is a CreateWorksheet stored procedure you can use to create a new worksheet or whole new workbook (spreadsheet file). There is no mechanism for modifying or dropping tables.

Working with Excel Formulas

The Excel Enterprise Connector does not require Excel to be installed to access Excel XLSX files. Instead the connector includes a formula engine that can natively calculate most of the commonly used Excel formulas. There are two connection properties that influence how the engine works: Recalculate lets you specify if the engine should intelligently recalculate the formulas, and AllowFormulas allow you turn on the ability to insert and modify formulas via SQL statements:

Fullscreen

1
INSERT INTO Excel_Sheet (A, B) VALUES (‘Bill’, ‘=SUM(B1:B5)’)
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Register Now

Register now to join the webinar and get your free Enterprise Connector licenses. The free Enterprise Connector licenses are offered for everyone who registers for the webinar and has an active Update Subscription license.

#ConnectTheData

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Director of Delphi Consulting for GDK Software USA. Many software related patents, including swipe and pattern unlock and search engines. First Silver and Gold Delphi badges on Stack Overflow Former Developer Advocate for Embarcadero Technologies. Long time fan of programming, especially with Delphi. Author, Podcaster/YouTuber, Improvisor, Public Speaker, Father, and Friend.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES