You've already forked lazarus-ccr
fpspreadsheet: Extend the event OnNeedCellData by a template cell which can be used for formatting when saving a file in virtual mode.
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3317 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -12,11 +12,18 @@ uses
|
|||||||
|
|
||||||
type
|
type
|
||||||
TDataProvider = class
|
TDataProvider = class
|
||||||
procedure NeedCellData(Sender: TObject; ARow,ACol: Cardinal; var AData: variant);
|
procedure NeedCellData(Sender: TObject; ARow,ACol: Cardinal;
|
||||||
|
var AData: variant; var AStyleCell: PCell);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
var
|
||||||
|
workbook: TsWorkbook;
|
||||||
|
worksheet: TsWorksheet;
|
||||||
|
dataprovider: TDataProvider;
|
||||||
|
headerTemplate: PCell;
|
||||||
|
|
||||||
procedure TDataProvider.NeedCellData(Sender: TObject; ARow, ACol: Cardinal;
|
procedure TDataProvider.NeedCellData(Sender: TObject; ARow, ACol: Cardinal;
|
||||||
var AData: variant);
|
var AData: variant; var AStyleCell: PCell);
|
||||||
{ This is just a sample using random data. Normally, in case of a database,
|
{ This is just a sample using random data. Normally, in case of a database,
|
||||||
you would read a record and return its field values, such as:
|
you would read a record and return its field values, such as:
|
||||||
|
|
||||||
@ -28,6 +35,13 @@ type
|
|||||||
s: String;
|
s: String;
|
||||||
n: Double;
|
n: Double;
|
||||||
begin
|
begin
|
||||||
|
if ARow = 0 then begin
|
||||||
|
AData := Format('Column %d', [ACol + 1]);
|
||||||
|
AStyleCell := headerTemplate;
|
||||||
|
// This makes the style of the "headerTemplate" cell available to
|
||||||
|
// formatting of all virtual cells in row 0.
|
||||||
|
// Important: The template cell must be an existing cell in the worksheet.
|
||||||
|
end else
|
||||||
if odd(random(10)) then begin
|
if odd(random(10)) then begin
|
||||||
s := Format('R=%d-C=%d', [ARow, ACol]);
|
s := Format('R=%d-C=%d', [ARow, ACol]);
|
||||||
AData := s;
|
AData := s;
|
||||||
@ -40,11 +54,6 @@ type
|
|||||||
WriteLn('Writing row ', ARow, '...');
|
WriteLn('Writing row ', ARow, '...');
|
||||||
end;
|
end;
|
||||||
|
|
||||||
var
|
|
||||||
workbook: TsWorkbook;
|
|
||||||
worksheet: TsWorksheet;
|
|
||||||
dataprovider: TDataProvider;
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
|
||||||
dataprovider := TDataProvider.Create;
|
dataprovider := TDataProvider.Create;
|
||||||
@ -58,24 +67,32 @@ begin
|
|||||||
|
|
||||||
// workbook.WritingOptions := [woVirtualMode, woSaveMemory];
|
// workbook.WritingOptions := [woVirtualMode, woSaveMemory];
|
||||||
workbook.WritingOptions := [woVirtualMode];
|
workbook.WritingOptions := [woVirtualMode];
|
||||||
|
{ woSaveMemory can be omitted, but is essential for large files: it causes
|
||||||
|
writing temporaray data to a file stream instead of a memory stream.
|
||||||
|
woSaveMemory, however, considerably slows down writing of biff files. }
|
||||||
|
|
||||||
// woSaveMemory can be omitted, but is essential for large files: it causes
|
{ Next two numbers define the size of virtual spreadsheet.
|
||||||
// writing temporaray data to a file stream instead of a memory stream.
|
In case of a database, VirtualRowCount is the RecordCount, VirtualColCount
|
||||||
// woSaveMemory, however, considerably slows down writing of biff files.
|
the number of fields to be written to the spreadsheet file }
|
||||||
|
|
||||||
workbook.VirtualRowCount := 10000;
|
workbook.VirtualRowCount := 10000;
|
||||||
workbook.VirtualColCount := 100;
|
workbook.VirtualColCount := 100;
|
||||||
// These two numbers define the size of virtual spreadsheet.
|
|
||||||
// In case of a database, VirtualRowCount is the RecordCount, VirtualColCount
|
|
||||||
// the number of fields to be written to the spreadsheet file
|
|
||||||
|
|
||||||
|
{ The event handler for OnNeedCellData links the workbook to the method
|
||||||
|
from which it gets the data to be written. }
|
||||||
workbook.OnNeedCellData := @dataprovider.NeedCellData;
|
workbook.OnNeedCellData := @dataprovider.NeedCellData;
|
||||||
// This links the worksheet to the method from which it gets the
|
|
||||||
// data to write.
|
|
||||||
|
|
||||||
// In case of a database, you would open the dataset before calling this:
|
{ If we want to change the format of some cells we have to provide this
|
||||||
|
format in template cells of the worksheet. In the example, the first
|
||||||
|
row whould be in bold letters and have a gray background.
|
||||||
|
Therefore, we define a "header template cell" and pass this in the
|
||||||
|
NeedCellData event handler.}
|
||||||
|
worksheet.WriteFontStyle(0, 0, [fssBold]);
|
||||||
|
worksheet.WriteBackgroundColor(0, 0, scSilver);
|
||||||
|
headerTemplate := worksheet.FindCell(0, 0);
|
||||||
|
|
||||||
|
{ In case of a database, you would open the dataset before calling this: }
|
||||||
workbook.WriteToFile('test_virtual.xlsx', sfOOXML, true);
|
workbook.WriteToFile('test_virtual.xlsx', sfOOXML, true);
|
||||||
// workbook.WriteToFile('test_virtual.xls', sfExcel5, true);
|
// workbook.WriteToFile('test_virtual.xls', sfExcel8, true);
|
||||||
|
|
||||||
finally
|
finally
|
||||||
workbook.Free;
|
workbook.Free;
|
||||||
|
@ -714,7 +714,7 @@ type
|
|||||||
TsWorkbookWritingOptions = set of TsWorkbookWritingOption;
|
TsWorkbookWritingOptions = set of TsWorkbookWritingOption;
|
||||||
|
|
||||||
TsWorkbookNeedCellDataEvent = procedure(Sender: TObject; ARow, ACol: Cardinal;
|
TsWorkbookNeedCellDataEvent = procedure(Sender: TObject; ARow, ACol: Cardinal;
|
||||||
var AValue: variant) of object;
|
var AValue: variant; var AStyleCell: PCell) of object;
|
||||||
|
|
||||||
{@@
|
{@@
|
||||||
The workbook contains the worksheets and provides methods for reading from
|
The workbook contains the worksheets and provides methods for reading from
|
||||||
|
@ -2533,12 +2533,15 @@ var
|
|||||||
r,c: Cardinal;
|
r,c: Cardinal;
|
||||||
lCell: TCell;
|
lCell: TCell;
|
||||||
value: variant;
|
value: variant;
|
||||||
|
styleCell: PCell;
|
||||||
begin
|
begin
|
||||||
for r := 0 to Workbook.VirtualRowCount-1 do begin
|
for r := 0 to Workbook.VirtualRowCount-1 do begin
|
||||||
for c := 0 to Workbook.VirtualColCount-1 do begin
|
for c := 0 to Workbook.VirtualColCount-1 do begin
|
||||||
FillChar(lCell, SizeOf(lCell), 0);
|
FillChar(lCell, SizeOf(lCell), 0);
|
||||||
value := varNull;
|
value := varNull;
|
||||||
Workbook.OnNeedCellData(Workbook, r, c, value);
|
styleCell := nil;
|
||||||
|
Workbook.OnNeedCellData(Workbook, r, c, value, styleCell);
|
||||||
|
if styleCell <> nil then lCell := styleCell^;
|
||||||
lCell.Row := r;
|
lCell.Row := r;
|
||||||
lCell.Col := c;
|
lCell.Col := c;
|
||||||
if VarIsNull(value) then
|
if VarIsNull(value) then
|
||||||
|
@ -743,6 +743,7 @@ var
|
|||||||
AVLNode: TAVLTreeNode;
|
AVLNode: TAVLTreeNode;
|
||||||
CellPosText: string;
|
CellPosText: string;
|
||||||
value: Variant;
|
value: Variant;
|
||||||
|
styleCell: PCell;
|
||||||
fn: String;
|
fn: String;
|
||||||
begin
|
begin
|
||||||
FCurSheetNum := Length(FSSheets);
|
FCurSheetNum := Length(FSSheets);
|
||||||
@ -779,7 +780,10 @@ begin
|
|||||||
FillChar(lCell, SizeOf(lCell), 0);
|
FillChar(lCell, SizeOf(lCell), 0);
|
||||||
CellPosText := CurSheet.CellPosToText(r, c);
|
CellPosText := CurSheet.CellPosToText(r, c);
|
||||||
value := varNull;
|
value := varNull;
|
||||||
Workbook.OnNeedCellData(Workbook, r, c, value);
|
styleCell := nil;
|
||||||
|
Workbook.OnNeedCellData(Workbook, r, c, value, styleCell);
|
||||||
|
if styleCell <> nil then
|
||||||
|
lCell := styleCell^;
|
||||||
lCell.Row := r;
|
lCell.Row := r;
|
||||||
lCell.Col := c;
|
lCell.Col := c;
|
||||||
if VarIsNull(value) then
|
if VarIsNull(value) then
|
||||||
|
Reference in New Issue
Block a user