Autofiting rows and columns (Delphi)
Note
This demo is available in your FlexCel installation at <FlexCel Install Folder>\Demo\Delphi\Modules\20.Reports\85.Autofit and also at https://github.com/tmssoftware/TMS-FlexCel.VCL-demos/tree/master/Delphi/Modules/20.Reports/85.Autofit
Overview
When using FlexCel, you can opt for autofit certain rows so they adjust to their content. By default, this is not done automatically for all rows since most rows do not need autofit, and so you need to use a special <#Row Height(autofit)> tag. Make sure you read Autofitting Rows and Columns in the API developer's guide for more information on autofitting.
Concepts
With default settings, autofitting rows in FlexCel will not change the autofitting status of the row. So, when you open this file in Excel, all row heights will be recalculated again, changing page breaks, but guaranteeing that all text is visible. If you prefer to keep autofitted row fixed when you open them on Excel, make the row fixed on the template or use the <#Autofit Settings> tag.
You can use an "Adjustment" parameter on the <#Row Height> and <#Autofit Settings> tags, to leave a margin for text. Remember that normally Excel text is a little wider than FlexCel's, so it might need more room. "AdjustmentFixed" is other parameter that can be used for the same thing.
You can use different types of autofit depending on your needs, and they are shown on different sheets on this demo. There are normally 2 "modes" of autofit:
The default. FlexCel autofits the rows so it can print/export fine, but leaves the rows to autofit so Excel will autofit them again when you open the file. This mode ensures text will not be "cropped" in Excel or FlexCel, but the row heights will be different in both.
"Fixed" mode. Here, FlexCel calculates the Autofit and fixes the row to that size. When you open the file in Excel rows will not change, so the page will print the same. But, as Excel normally uses more space to print the words, some words might be cropped when opening on Excel. To avoid cropping, it is recommended that you use a large Adjustment, of about 150.
Files
UMainForm.pas
unit UMainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
FlexCel.VCLSupport, FlexCel.Core, FlexCel.XlsAdapter, FlexCel.Report, FlexCel.Render,
{$if CompilerVersion >= 23.0} System.UITypes, {$IFEND}
ShellApi,
Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type
TMainForm = class(TForm)
btnCancel: TButton;
btnGo: TButton;
SaveDialog: TSaveDialog;
Label1: TLabel;
procedure btnCancelClick(Sender: TObject);
procedure btnGoClick(Sender: TObject);
private
procedure RunReport;
function GetDataPath: string;
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
uses IOUtils, DemoData;
{$R *.dfm}
procedure TMainForm.btnCancelClick(Sender: TObject);
begin
Close;
end;
procedure TMainForm.btnGoClick(Sender: TObject);
begin
RunReport;
end;
function TMainForm.GetDataPath: string;
begin
Result := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), '..\..');
end;
procedure TMainForm.RunReport;
var
Report: TFlexCelReport;
begin
if not SaveDialog.Execute then exit;
Report := TFlexCelReport.Create(true);
try
Report.AddTable(DemoTables);
Report.SetValue('Date', Now);
Report.Run(
TPath.Combine(GetDataPath, 'Autofit.template.xls'),
SaveDialog.FileName);
finally
Report.Free;
end;
if MessageDlg('Do you want to open the generated file?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
ShellExecute(0, 'open', PCHAR(SaveDialog.FileName), nil, nil, SW_SHOWNORMAL);
end;
end;
end.