A powerful Delphi unit for seamless interaction with Microsoft Excel, providing a comprehensive set of procedures and functions to manipulate Excel files programmatically. Compatible with modern Delphi versions.
Overview
DFExcel (Delphi-Fast Excel) simplifies Excel automation in Delphi applications by providing a clean, object-oriented interface to Microsoft Excel's COM objects. It handles common operations like reading/writing data, formatting cells, managing worksheets, and performing calculations, while abstracting away the complexities of the Excel object model.
Features
Simple Excel Instance Management: Create, open, save, and close Excel workbooks with minimal code
Comprehensive Cell Operations: Read and write values of any type (string, integer, float, boolean, date)
Worksheet Management: Create, delete, rename, and navigate between worksheets
Range Manipulation: Select, copy, paste, and format cell ranges
Formula Support: Apply formulas and perform calculations
Formatting Options: Apply number formats, fonts, borders, colors
Data Import/Export: Import from and export to various formats including CSV and text files
Chart Creation: Generate charts from data ranges
Error Handling: Robust error capture and reporting system
Modern Delphi Support: Compatible with current Delphi versions including Delphi 10.x, 11.x and beyond
Requirements
Delphi 10.x or higher (also works with older versions with minor modifications)
Microsoft Excel installed on the system
Windows operating system
Installation
Clone this repository or download the source code
Add the
DFExcel.pasunit to your Delphi projectAdd
DFExcelto your uses clauseMake sure your project includes the required COM dependencies
Quick Start
uses
DFExcel;
var
Excel: TDFExcelApplication;
Workbook: TDFExcelWorkbook;
Sheet: TDFExcelWorksheet;
begin
// Initialize Excel application
Excel := TDFExcelApplication.Create;
try
// Create a new workbook
Workbook := Excel.CreateWorkbook;
// Get the first worksheet
Sheet := Workbook.Worksheets[1];
// Write data to cells
Sheet.Cells[1, 1].Value := 'Product';
Sheet.Cells[1, 2].Value := 'Quantity';
Sheet.Cells[1, 3].Value := 'Price';
Sheet.Cells[2, 1].Value := 'Widget';
Sheet.Cells[2, 2].Value := 100;
Sheet.Cells[2, 3].Value := 19.99;
// Apply formula
Sheet.Cells[2, 4].Formula := '=B2*C2';
// Format header row
Sheet.Range['A1:D1'].Font.Bold := True;
// Save workbook
Workbook.SaveAs('SampleReport.xlsx');
finally
// Clean up resources
Excel.Free;
end;
end;API Reference
TDFExcelApplication
The main class representing an Excel application instance.
Methods
Create(Visible: Boolean = False): Creates a new Excel application instanceCreateWorkbook: Creates a new workbookOpenWorkbook(FileName: string): Opens an existing workbookQuit: Closes Excel applicationDisplayAlerts(Value: Boolean): Controls whether Excel displays alertsScreenUpdating(Value: Boolean): Controls screen updating for performanceCalculate: Calculates all open workbooksEvaluate(Name: string): Evaluates a named formula or range
Properties
Visible: Sets Excel visibility (boolean property)ActiveWorkbook: Gets the currently active workbookWorkbooks[Index: Integer]: Access workbooks by indexWorkbookCount: Returns the number of open workbooks
TDFExcelWorkbook
Represents an Excel workbook.
Methods
AddWorksheet(Name: string = ''): Adds a new worksheetWorksheetByName(Name: string): Access worksheet by nameSave: Saves the workbookSaveAs(FileName: string; FileFormat: Integer = xlOpenXMLWorkbook): Saves the workbook with the specified nameClose(SaveChanges: Boolean = True): Closes the workbook
Properties
Worksheets[Index: Integer]: Access worksheets by indexWorksheetCount: Returns the number of worksheetsFileName: Returns the full path of the workbook
TDFExcelWorksheet
Represents an Excel worksheet.
Methods
Cells[Row, Col: Integer]: Access individual cellsRange[Address: string]: Access a range of cellsCopySheet(After: OleVariant): Copy this worksheetDelete: Delete this worksheetActivate: Make this worksheet activeInsertRow(RowIndex: Integer): Insert a rowInsertColumn(ColIndex: Integer): Insert a columnDeleteRow(RowIndex: Integer): Delete a rowDeleteColumn(ColIndex: Integer): Delete a columnCreateChart(ChartType: TDFExcelChartType; DataRange: string): Create a chartPrintOut(From: Integer = 0; To_: Integer = 0; Copies: Integer = 1): Print the worksheetPrintPreview: Preview the worksheet for printing
Properties
Name: The worksheet nameUsedRange: The range of cells that contains dataVisible: Controls worksheet visibility
TDFExcelRange
Represents a range of Excel cells.
Methods
Clear: Clears all content and formattingClearContents: Clears only the contentClearFormats: Clears only the formattingAutoFit: Auto-fits columns to contentMerge: Merges cells in the rangeUnmerge: Unmerges previously merged cellsCopy: Copies the range to the clipboardCut: Cuts the range to the clipboardPaste: Pastes from the clipboardPasteSpecial(Format: Integer = xlPasteAll): Pastes with specific optionsSort(Key1Range, Key2Range, ...): Sorts the rangeCreateFormatCondition(...): Creates conditional formatting
Properties
Value: Gets or sets the cell values (variant)Formula: Gets or sets formulasHorizontalAlignment: Gets or sets horizontal alignmentVerticalAlignment: Gets or sets vertical alignmentFont: Access font propertiesInterior: Access cell background propertiesBorders: Access cell border properties
TDFExcelCell
Represents an individual Excel cell.
Properties
Value: Gets or sets the cell value (variant)Formula: Gets or sets a formulaNumberFormat: Gets or sets number format stringHorizontalAlignment: Gets or sets horizontal alignmentVerticalAlignment: Gets or sets vertical alignmentFont: Access font propertiesInterior: Access cell background propertiesBorders: Access cell border properties
TDFExcelChart
Represents an Excel chart.
Methods
SetSourceData(Range: OleVariant; PlotBy: Integer = xlColumns): Set the chart data sourceExport(FileName: string; FilterName: string = 'PNG'): Export chart as image
Properties
ChartType: Gets or sets the chart typeHasTitle: Gets or sets whether the chart has a titleChartTitle: Access the chart title propertiesHasLegend: Gets or sets whether the chart has a legend
Advanced Features
Range Formatting
// Apply formatting to a range
var Range: TDFExcelRange;
begin
Range := Sheet.Range['A1:D10'];
// Apply borders
Range.Borders.LineStyle := xlContinuous;
Range.Borders.Weight := xlThin;
// Apply background color
Range.Interior.Color := clLightBlue;
// Apply text alignment
Range.HorizontalAlignment := xlCenter;
end;Creating Charts
// Create a chart from data
var Chart: TDFExcelChart;
begin
// Specify data range and chart type
Chart := Sheet.CreateChart(ctBar, 'A1:B10');
// Configure chart properties
Chart.HasTitle := True;
Chart.ChartTitle.Text := 'Sales Report';
Chart.HasLegend := True;
end;Batch Operations
// Import data from array
var
DataArray: Variant;
i, j: Integer;
begin
// Create a 2D array
DataArray := VarArrayCreate([1, 100, 1, 5], varVariant);
// Fill with data
for i := 1 to 100 do
for j := 1 to 5 do
DataArray[i, j] := i * j;
// Write entire array to Excel in one operation
Sheet.Range['A1:E100'].Value := DataArray;
end;Performance Tips
Disable screen updating during batch operations:
Excel.ScreenUpdating(False);
try
// Perform operations
finally
Excel.ScreenUpdating(True);
end;Use arrays for bulk operations instead of setting cells individually:
// Instead of:
for i := 1 to 1000 do
Sheet.Cells[i, 1].Value := i;
// Use:
var Data: Variant;
Data := VarArrayCreate([1, 1000, 1, 1], varVariant);
for i := 1 to 1000 do
Data[i, 1] := i;
Sheet.Range['A1:A1000'].Value := Data;Avoid excessive property access across COM boundaries:
// Instead of:
for i := 1 to 100 do begin
Sheet.Cells[i, 1].Font.Bold := True;
Sheet.Cells[i, 1].Font.Size := 12;
end;
// Use:
Sheet.Range['A1:A100'].Font.Bold := True;
Sheet.Range['A1:A100'].Font.Size := 12;Error Handling
DFExcel includes comprehensive error handling to gracefully manage Excel-related exceptions:
try
// Excel operations
Sheet.Cells[1, 1].Value := 'Test';
except
on E: EDFExcelException do
ShowMessage('Excel error: ' + E.Message);
on E: Exception do
ShowMessage('General error: ' + E.Message);
end;Best Practices
Always properly clean up Excel instances to prevent memory leaks
Use batch operations when handling large data sets
Use structured references when working with Excel tables
Handle COM errors appropriately
Consider creating a separate thread for time-consuming Excel operations
Be aware that some operations may trigger Excel's modal dialogs
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgements
Thanks to the Delphi community for feedback and suggestions
Special thanks to Microsoft for documenting the Excel Object Model
