FiveWin provides three complementary strategies for Excel file integration, ranging from
high-speed native generation using the xlsxlibhb C library (based on
libxlsxwriter), to OLE automation via TOleAuto for full Excel COM
control, and direct XML parsing for reading .xlsx files without Excel.
Strategy Overview
Strategy
Source
Use Case
Native XLSX (TWorkBook/TWorkSheet)
fwxlsxlb.prg
High-performance creation, no Excel required, supports formatting and images
OLE Automation (FWDrXLSX)
drxl.prg
Full Excel COM object model for legacy formatting and advanced features
XML Reader (TXlsxReader)
readxlsx.prg
Reading .xlsx files via ZIP/XML parsing, no Excel required
TWorkBook / TWorkSheet - Native XLSX Generation
The TWorkBook and TWorkSheet classes provide an object-oriented
interface to the xlsxlibhb C library. This is the preferred method for generating
large spreadsheets quickly without any dependency on Microsoft Excel.
TWorkBook Class
Method
Description
New( cFile )
Create a new workbook. If the file exists, it is erased before creating a new one. Returns nil on failure.
AddSheet( cName )
Add a new worksheet with the given name. Returns a TWorkSheet object.
Format( cPicture, cStyles, nAlign )
Create a TFormat object for cell formatting.
Close()
Finalize and save the workbook to disk via workbook_close().
TWorkSheet Class
Method
Description
Say( nRow, nCol, uValue, oFormat, cPicture )
Write a value to a cell. Automatically detects Harbour type: numeric → worksheet_write_number(), date/datetime → worksheet_write_datetime(), logical → worksheet_write_boolean(), string → worksheet_write_string() with Ansi-to-UTF-8 conversion. Also accepts cell references like "A1" as row parameter.
SayImage( nRow, nCol, cImage )
Insert an image into a cell. Supports file paths and image buffers. Scales to fit aImageSize (default 200x200px).
The TXlsxReader class reads .xlsx files by treating the file as a ZIP
archive and parsing the internal OpenXML components. No Excel installation is required.
Method
Description
New( cXlsx, cSheet )
Open an .xlsx file for reading. Optionally specify a sheet name. Returns nil if the file is not found or invalid.
ReadData()
Read all data into aData (2D array). Automatically detects types (N, D, T, L, C, M) and computes structure.
ShowData()
Display the spreadsheet in an XBrowse dialog with automatic type detection.
Range( cRange )
Read a specific range (e.g., "A1:C10") as a 2D array.
Cells( nRow, nCol )
Read a single cell or a set of columns from a specific row.
SaveAs( cSave )
Save the loaded data to a new .xlsx file using the native XLSX library.
Close()
Release internal resources.
Data Members
DATA
Type
Description
nRows
Numeric
Number of rows in the sheet
nCols
Numeric
Number of columns in the sheet
aHead
Array
Header row (first row, if all character values)
aStruct
Array
Column structure (name, type, length, decimals)
aData
Array
All data rows (populated after ReadData())
nSecs
Numeric
Elapsed seconds for reading (read-only)
The two convenience functions wrap the reader for one-liner usage:
Function
Description
FW_ShowXlsx( cXlsx, cSheet )
Open and display an .xlsx file in an XBrowse browser dialog.
FW_OpenXlsx( cXlsx, cSheet )
Open an .xlsx file and return a TXlsxReader object for programmatic access.
OLE Automation (FWDrXLSX)
For environments where Microsoft Excel is installed, the FWDrXLSX class wraps
Excel's COM object model. The class is loaded dynamically via HB_FuncPtr() to
avoid hard dependencies.
Method
Description
New( cFile )
Create a new Excel file via OLE. Prompts for a filename if not provided.
Say( nRow, nCol, uValue, oFormat, cPicture )
Write a value to a cell. Accepts both numeric coordinates and cell references (e.g., "A1").
Format( cPicture, cStyles, nAlign )
Create a format object (FWDrXLSXFormat) for the OLE sheet.
SayRow( nRow, aValues, oFormat, aPicture )
Write an array of values to a row.
SayHeader( nRow, aHead )
Write a header row with bold formatting and bottom border.
SetColSizes( aSizes )
Set column widths from a structure array.
SetStruct( aStruct, nRow, aGroup )
Define a structured table with headers and column sizing.
SetArrayData( aData, aStruct, aGroup )
Write a 2D array as a formatted table.
CreateFrom( uSrc )
Import data from the current DBF alias or an ADO record set.
Runtime linking and selection logic:
Function
Description
UseDrXlsx( lSet )
Returns .T. if OLE automation should be used (drxl.prg linked and Excel not installed). Optionally set the preference with a logical parameter.
DrXlsxLinked()
Returns .T. if the FWDrXLSX class is available at runtime.
DrXlsxObj( cFileXlsx )
Create and return an FWDrXLSX object for the given file.
DrXlsFormat( oXlsx )
Create a format object attached to an FWDrXLSX instance.
Utility Functions
Function
Description
XLSXLIBLinked()
Returns .T. if the xlsxlibhb native library is available (checks for XLSXLIB or TWORKBOOK function pointers).
UseXLSXLIB( lSet )
Returns .T. if native xlsxlibhb should be preferred over OLE. Optionally set the preference.
XlsxLibObj( cFile, cSheet )
Create a TWorkSheet object using the native library.
XlsxObj( cFile, cSheet )
Smart factory: tries native library first, falls back to OLE, then back to native. Returns a usable worksheet object or nil.
cTempXlsx()
Generate a unique temporary filename with .xlsx extension in the system temp directory.
cGetFileXlsx( lTmp )
Open a standard file-save dialog for .xlsx files. If lTmp is .T. and the user cancels, returns a temporary filename.
#include "FiveWin.ch"
function ReadXlsx()
local oXlsx := FW_OpenXlsx( "c:\temp\example.xlsx" )
if oXlsx == nil
MsgStop( "Cannot open file" )
return nil
endif
? "Rows:", oXlsx:nRows, "Cols:", oXlsx:nCols
? "Header:", oXlsx:aHead[ 1 ], oXlsx:aHead[ 2 ], oXlsx:aHead[ 3 ]
// Access specific cell
MsgInfo( oXlsx:Cells( 2, 2 ), "Cell B2" )
// Read a range
XBROWSER oXlsx:Range( "A1:D3" ) TITLE "Range A1:D3"
oXlsx:Close()
return nil
Notes
Native XLSX (TWorkBook) is the recommended approach for creating
spreadsheets. It has no external dependencies, supports large files, and provides full
formatting control through the TFormat class.
OLE Automation (FWDrXLSX) requires Microsoft Excel to be
installed on the target machine. It is suitable for legacy code or when Excel-specific
features (macros, pivot tables) are needed.
TXlsxReader automatically detects data types from the Excel XML metadata,
handling strings (shared strings table), numbers, dates, datetimes, booleans, and images.
The cTempXlsx() and cGetFileXlsx() functions handle temporary file
management and conflict detection (files currently open in Excel cannot be overwritten).
When both native and OLE libraries are available, UseXLSXLIB() and
UseDrXlsx() control which strategy is preferred. By default, the native library
is chosen when Excel is not installed.
String values in native XLSX are automatically converted from ANSI to UTF-8 for proper
encoding in the .xlsx XML. Binary memo data is detected and replaced with
"<binary data>".