File: le_e_spreadsheet.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (596 lines) | stat: -rw-r--r-- 19,359 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
{
  LazReport matrix for export reports

 Copyright (C) 2014-2015 alexs alexs75.at.yandex.ru

 The module is designed to create an image of the report with the exact
 positioning of objects and subsequent binding to the worksheet

 This library is free software; you can redistribute it and/or modify it
 under the terms of the GNU Library General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version with the following modification:

 As a special exception, the copyright holders of this library give you
 permission to link this library with independent modules to produce an
 executable, regardless of the license terms of these independent modules,and
 to copy and distribute the resulting executable under terms of your choice,
 provided that you also meet, for each linked independent module, the terms
 and conditions of the license of that module. An independent module is a
 module which is not derived from or based on this library. If you modify
 this library, you may extend this exception to your version of the library,
 but you are not obligated to do so. If you do not wish to do so, delete this
 exception statement from your version.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
 for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; if not, write to the Free Software Foundation,
 Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}

unit le_e_spreadsheet;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LR_ExportMatrix, LR_Class, LR_BarC, fpspreadsheet, Graphics, le_e_spreadsheet_types;

type

  { TlrSpreadSheetExportFilter }

  TlrSpreadSheetExportFilter = class(TfrExportFilter)
  private
    FDataGrouping: TlreDataGrouping;
    FDataGroupingChunks: integer;
    FDeleteEmptyRow: boolean;
    FExportMatrix:TExportMatrix;
    FExportPrintRange: boolean;
    FExportImages: boolean;
    FExportURL: boolean;
    FMergeCell: boolean;
    FOpenAfterExport: boolean;
    FWorkbook:TsWorkbook;
    FWorksheet:TsWorksheet;
    FFileName:string;
    FCurPage:integer;
    FTmpTextWidth: Double;
    FTmpTextHeight: Double;
    FTempMemStreem: TStream;
    procedure ExportColWidth;
    procedure ExportRowHight;
    procedure DoExportPrintRange;
    procedure ExportData1;

    procedure MakeWorksheet;
    function GetTempMemStreem(AClear:boolean):TStream;
    procedure ShowBarCode(View: TfrCustomBarCodeView);
  protected
    function Setup:boolean; override;
    procedure AfterExport; override;
  public
    constructor Create(AStream: TStream); override;
    destructor Destroy; override;
    procedure OnBeginPage; override;
    procedure OnEndPage; override;
    procedure OnBeginDoc; override;
    procedure OnEndDoc; override;
{    procedure ShowBackGround(View: TfrView; x, y, h, w: integer);
    procedure Frame(View: TfrView; x, y, h, w: integer);
    procedure ShowFrame(View: TfrView; x, y, h, w: integer);
    procedure Line(View: TfrView; x1,y1, x2,y2: Integer);
    procedure ShowPicture(View: TfrPictureView; x, y, h, w: integer);
    procedure ShowRoundRect(View: TfrRoundRectView; x, y, h, w: integer);
    procedure ShowShape(View: TfrShapeView; x, y, h, w: integer);
    procedure OnText(X, Y: Integer; const Text: string; View: TfrView); override;
    procedure OnData(x, y: Integer; View: TfrView); override;}
    procedure OnExported(x, y: Integer; View: TfrView); override;
  public
    property DataGrouping:TlreDataGrouping read FDataGrouping write FDataGrouping;
    property DataGroupingChunks:integer read FDataGroupingChunks write FDataGroupingChunks;
    property OpenAfterExport:boolean read FOpenAfterExport write FOpenAfterExport;
    property DeleteEmptyRow:boolean read FDeleteEmptyRow write FDeleteEmptyRow;
    property MergeCell:boolean read FMergeCell write FMergeCell;
    property ExportURL:boolean read FExportURL write FExportURL;
    property ExportPrintRange:boolean read FExportPrintRange write FExportPrintRange;
    property ExportImages:boolean read FExportImages write FExportImages;
  end;

implementation
uses LCLType, le_e_spreadsheet_params, fpsTypes, fpsutils, fpsAllFormats,
  LazUTF8Classes, Forms, Controls, LCLIntf, LazFileUtils, le_e_spreadsheet_consts,
  lrSpreadSheetExp, math, fpsPageLayout;

const
  ssAligns : array [TAlignment] of TsHorAlignment = (haLeft, haRight, haCenter);
  ssLayout : array [TTextLayout] of TsVertAlignment = (vaTop, vaCenter, vaBottom);

  { TlrSpreadSheetExportFilter }

procedure TlrSpreadSheetExportFilter.ExportColWidth;
var
  i: Integer;
begin
  for i:=0 to FExportMatrix.ColumnCount-1 do
    //FWorksheet.WriteColWidth(i, round(FExportMatrix.ColumnWidth[i] * 0.161404639));
    //FWorksheet.WriteColWidth(i, Max(FExportMatrix.ColumnWidth[i] / FTmpTextWidth, 1));
    FWorksheet.WriteColWidth(i, FExportMatrix.ColumnWidth[i] / FTmpTextWidth);
end;

procedure TlrSpreadSheetExportFilter.ExportRowHight;
var
  i: Integer;
begin
  for i:=0 to FExportMatrix.RowCount - 1 do
    FWorksheet.WriteRowHeight(i, FExportMatrix.RowHiht[i] / FTmpTextHeight);
end;

procedure TlrSpreadSheetExportFilter.DoExportPrintRange;
var
  X1, Y1, X2, Y2, R, C: Integer;
  Row: TExportRow;
  Cel: TExportObject;
begin
  X1:=MaxInt;
  Y1:=MaxInt;
  X2:=-1;
  Y2:=-1;
  for R:=0 to FExportMatrix.Rows.Count-1 do
  begin
    Row:=TExportRow(FExportMatrix.Rows[R]);
    for C:=0 to Row.Cells.Count-1 do
    begin
      Cel:=TExportObject(Row.Cells[C]);
      if Assigned(Cel) then
      begin
        if Cel.Col < X1 then
          X1:=Cel.Col;
        if Cel.Row < Y1 then
          Y1:=Cel.Row;

        if (Cel.Col < Cel.MergedCol) then
        begin
          if Cel.MergedCol > X2 then
            X2:=Cel.MergedCol;
        end
        else
        if Cel.Col > X2 then
          X2:=Cel.Col;


        if (Cel.Row < Cel.MergedRow) then
        begin
          if Cel.MergedRow > Y2 then
            Y2:=Cel.MergedRow;
        end
        else
        if Cel.Row > Y2 then
          Y2:=Cel.Row;
      end;
    end;
  end;

  if (X1>0) and (Y1>0) and (X2>X1) and (Y2>Y1) then
  begin
    FWorksheet.PageLayout.AddPrintRange(Y1, X1, Y2, X2);
  end;
end;

function sftofs(AFont:TFont):TsFontStyles;
begin
  Result:=[];
  if fsBold in AFont.Style then
    Result:=Result + [fssBold];

  if fsItalic in AFont.Style then
    Result:=Result + [fssItalic];

  if fsStrikeOut in AFont.Style then
    Result:=Result + [fssStrikeOut];

  if fsUnderline in AFont.Style then
    Result:=Result + [fssUnderline];
end;

procedure TlrSpreadSheetExportFilter.ExportData1;
var
  R: Integer;
  Row: TExportRow;
  C: Integer;
  Cel: TExportObject;
  X: Integer;
  Y: Integer;
  scFrm:TsCellBorders;
  FS: TStream;
  FISF: Extended;
begin
  for R:=0 to FExportMatrix.Rows.Count-1 do
  begin
    Row:=TExportRow(FExportMatrix.Rows[R]);
    for C:=0 to Row.Cells.Count-1 do
    begin
      Cel:=TExportObject(Row.Cells[C]);

      if Assigned(Cel) then
      begin
        X:=Cel.Col;
        Y:=Row.Row;
        if (Cel.ObjType = gtMemo) and (Cel.Text<>'') then
        begin
          FWorksheet.WriteUTF8Text(Y, X, TrimRight(Cel.Texts.Text));
          if Cel.Angle <> 0 then
            FWorksheet.WriteTextRotation(Y, X, rt90DegreeCounterClockwiseRotation);
          FWorksheet.WriteVertAlignment(Y, X, ssLayout[Cel.Layout]);
          FWorksheet.WriteWordwrap(Y, X, Cel.WordWrap);

          FWorksheet.WriteBackgroundColor(Y, X, Cel.FillColor);
        end
        else
        if (Cel.ObjType = gtPicture) and (Assigned(Cel.Picture)) then
        begin
          // this code for export images - not work. in progress
          FS:=GetTempMemStreem(true);
          Cel.Picture.SaveToStream(FS);
          if (FS.Size > 0) and (Cel.Widht > 0) and (Cel.Height > 0) then
          begin
            FS.Position:=0;
            FISF:=2.1;  // empiric value :-(
            FWorksheet.WriteImage(Y, X, FS, 0, 0, Cel.Picture.Width / Cel.Widht / FISF, Cel.Picture.Height / Cel.Height / FISF);
          end;
        end;


        if (Cel.Col < Cel.MergedCol) or (Cel.Row < Cel.MergedRow) then
          FWorksheet.MergeCells(Y, X, Cel.MergedRow, Cel.MergedCol);

        if Assigned(Cel.Font) and (Cel.Font.Size > 0) then
          FWorksheet.WriteFont(Y, X, Cel.Font.Name,  Cel.Font.Size, sftofs(Cel.Font), Cel.Font.Color, fpNormal);

        FWorksheet.WriteHorAlignment(Y, X, ssAligns[Cel.Alignment]);

        scFrm:=[];
        if frbLeft in Cel.Frames then
        begin
          FWorksheet.WriteBorderColor(Y, X, cbEast, Cel.FrameColor);
          scFrm:=scFrm + [cbEast]
        end;
        if frbTop in Cel.Frames then
        begin
          FWorksheet.WriteBorderColor(Y, X, cbNorth, Cel.FrameColor);
          scFrm:=scFrm + [cbNorth]
        end;
        if frbBottom in Cel.Frames then
        begin
          FWorksheet.WriteBorderColor(Y, X, cbSouth, Cel.FrameColor);
          scFrm:=scFrm + [cbSouth]
        end;
        if frbRight in Cel.Frames then
        begin
          FWorksheet.WriteBorderColor(Y, X, cbWest, Cel.FrameColor);
          scFrm:=scFrm + [cbWest]
        end;

        if scFrm <> [] then
          FWorksheet.WriteBorders(Y, X, scFrm);

        if FExportURL and  (Cel.URLInfo <> '') then
          FWorksheet.WriteHyperlink(Y, X, Cel.URLInfo, Cel.URLInfo);
      end;
    end;
  end;
end;

procedure TlrSpreadSheetExportFilter.MakeWorksheet;
var
  S: String;
begin
  FExportMatrix.PrepareData;
  if FDataGrouping = ldgLikeReport then
    S:=Format(sPageName, [FCurPage])
  else
    S:=sReportPageName;

  FWorksheet := FWorkbook.AddWorksheet(S);

  ExportColWidth;
  ExportRowHight;
  ExportData1;
  if FExportPrintRange then
  begin
    DoExportPrintRange;
    FWorksheet.PageLayout.Options := FWorksheet.PageLayout.Options + [poFitPages];
    FWorksheet.PageLayout.FitWidthToPages := 1;     // all columns on one page width
    FWorksheet.PageLayout.FitHeightToPages := 0;
  end;

  FWorksheet:=nil;
  FExportMatrix.Clear;
end;

function TlrSpreadSheetExportFilter.GetTempMemStreem(AClear: boolean): TStream;
begin
  if FTempMemStreem = nil then
    FTempMemStreem:=TMemoryStream.Create
  else
  if AClear then
    TMemoryStream(FTempMemStreem).Clear;

  Result:=FTempMemStreem;
end;

procedure TlrSpreadSheetExportFilter.ShowBarCode(View: TfrCustomBarCodeView);
var
  R: TExportObject;
  FBmp: TBitmap;
  FX, FY: Integer;
  LHeader: ^TBitMapInfoHeader;
  M: TMemoryStream;
const
  F_DPI = 60; //empiric value :-(
{
+  procedure BitmapSaveToFile(const ABitmap: TBitmap; const AFileName: TFileName;
+    const ADPI: Integer);
+  var
+    LMemoryStream: TMemoryStream;
+    LHeader: ^TBitMapInfoHeader;
+  begin
+    LMemoryStream := TMemoryStream.Create;
+    try
+      ABitmap.SaveToStream(LMemoryStream);
+      LHeader := LMemoryStream.Memory + SizeOf(TBitMapFileHeader);
+      LHeader^.biXPelsPerMeter := Ceil(ADPI * 100 / 2.54);
+      LHeader^.biYPelsPerMeter := LHeader^.biXPelsPerMeter;
+      LMemoryStream.SaveToFile(AFileName);
+    finally
+      FreeAndNil(LMemoryStream);
+    end;
+  end;
+}
begin
  R:=FExportMatrix.ExportObject(View);
  FBmp:=TBitmap.Create;
  FBmp.Width:=View.dx;
  FBmp.Height:=View.dy;
  FBmp.Canvas.Brush.Color:=clWhite;
  FBmp.Canvas.FillRect(0, 0, FBmp.Width, FBmp.Height);
  FBmp.Canvas.Pen.Color:=clBlack;
  FBmp.Canvas.Line(1, 1 , 10, 10 );

  FX:=View.X;
  FY:=View.Y;
  View.X:=0;
  View.Y:=0;

  View.Draw(FBmp.Canvas);

  View.X:=FX;
  View.Y:=FY;

  M:=TMemoryStream.Create;
  FBMP.SaveToStream(M);

  LHeader := M.Memory + SizeOf(TBitMapFileHeader);
  LHeader^.biXPelsPerMeter := Ceil(F_DPI * 100 / 2.54);
  LHeader^.biYPelsPerMeter := LHeader^.biXPelsPerMeter;
  M.Position:=0;
  R.NeedPicture;
  M.Position:=0;
  R.Picture.LoadFromStream(M);

  R.ObjType:=gtPicture;
  FBmp.Free;
end;

function TlrSpreadSheetExportFilter.Setup: boolean;
begin
  Result:=inherited Setup;

  if Assigned(lrSpreadSheetExportComponent) and not lrSpreadSheetExportComponent.ShowSetupForm then exit;

  leSpreadsheetParamsForm:=TleSpreadsheetParamsForm.Create(Application);
  leSpreadsheetParamsForm.LikeReportRadioButton.Checked:=FDataGrouping = ldgLikeReport;
  leSpreadsheetParamsForm.AllInOnePageRadioButton.Checked:=FDataGrouping = ldgAllInOnePage;
  leSpreadsheetParamsForm.RowsPerChunkRadioButton.Checked:=FDataGrouping = ldgChunks;
  leSpreadsheetParamsForm.RowsPerChunkEdit.Value:=FDataGroupingChunks;
  leSpreadsheetParamsForm.OpenAfterExportCheckBox.Checked:=FOpenAfterExport;
  leSpreadsheetParamsForm.MergeCellsCheckBox.Checked:=FMergeCell;
  leSpreadsheetParamsForm.DeleteEmptyRowsCheckBox.Checked:=FDeleteEmptyRow;
  leSpreadsheetParamsForm.ExportPicturesCheckBox.Checked:=FExportImages;
  leSpreadsheetParamsForm.ExportURLCheckBox.Checked:=FExportURL;
  leSpreadsheetParamsForm.ExportPrintRangeCheckBox.Checked:=FExportPrintRange;
  leSpreadsheetParamsForm.ExportReportTitleCheckBox.Checked:=btReportTitle in BandTypes;
  leSpreadsheetParamsForm.ExportReportSummaryCheckBox.Checked:=btReportSummary in BandTypes;
  leSpreadsheetParamsForm.ExportPageHeaderCheckBox.Checked:=btPageHeader in BandTypes;
  leSpreadsheetParamsForm.ExportPageFooterCheckBox.Checked:=btPageFooter in BandTypes;

  Result:=leSpreadsheetParamsForm.ShowModal = mrOk;
  if Result then
  begin
    if leSpreadsheetParamsForm.LikeReportRadioButton.Checked then
      FDataGrouping:=ldgLikeReport
    else
    if leSpreadsheetParamsForm.AllInOnePageRadioButton.Checked then
      FDataGrouping:=ldgAllInOnePage
    else
    begin
      FDataGrouping:=ldgChunks;
      FDataGroupingChunks:=leSpreadsheetParamsForm.RowsPerChunkEdit.Value;
    end;
    FExportImages   := leSpreadsheetParamsForm.ExportPicturesCheckBox.Checked;
    FOpenAfterExport:= leSpreadsheetParamsForm.OpenAfterExportCheckBox.Checked;
    FMergeCell      := leSpreadsheetParamsForm.MergeCellsCheckBox.Checked;
    FDeleteEmptyRow := leSpreadsheetParamsForm.DeleteEmptyRowsCheckBox.Checked;
    FExportURL      := leSpreadsheetParamsForm.ExportURLCheckBox.Checked;
    FExportPrintRange:=leSpreadsheetParamsForm.ExportPrintRangeCheckBox.Checked;

    FExportMatrix.MergeCell:=FMergeCell;
    FExportMatrix.DeleteEmptyRow:=FDeleteEmptyRow;

    if leSpreadsheetParamsForm.ExportReportTitleCheckBox.Checked then
      BandTypes := BandTypes + [btReportTitle]
    else
      BandTypes := BandTypes - [btReportTitle];
    if leSpreadsheetParamsForm.ExportReportSummaryCheckBox.Checked then
      BandTypes := BandTypes + [btReportSummary]
    else
      BandTypes := BandTypes - [btReportSummary];
    if leSpreadsheetParamsForm.ExportPageHeaderCheckBox.Checked then
      BandTypes := BandTypes + [btPageHeader]
    else
      BandTypes := BandTypes - [btPageHeader];
    if leSpreadsheetParamsForm.ExportPageFooterCheckBox.Checked then
      BandTypes := BandTypes + [btPageFooter]
    else
      BandTypes := BandTypes - [btPageFooter];
  end;
  leSpreadsheetParamsForm.Free;
end;

procedure TlrSpreadSheetExportFilter.AfterExport;
begin
  inherited AfterExport;
  if FOpenAfterExport and FileExistsUTF8(FFileName) then
    OpenDocument(FFileName);
end;

constructor TlrSpreadSheetExportFilter.Create(AStream: TStream);
begin
  inherited Create(AStream);

  if Assigned(lrSpreadSheetExportComponent) then
  begin
    FDataGrouping:=lrSpreadSheetExportComponent.DataGrouping;
    FDataGroupingChunks:=lrSpreadSheetExportComponent.DataGroupingChunks;
    FOpenAfterExport:=lrSpreadSheetExportComponent.OpenAfterExport;
    FMergeCell:=lrSpreadSheetExportComponent.MergeCell;
    FDeleteEmptyRow:=lrSpreadSheetExportComponent.DeleteEmptyRow;
    FExportURL:=lrSpreadSheetExportComponent.ExportURL;
    FExportPrintRange:=lrSpreadSheetExportComponent.ExportPrintRange;
    FExportImages:=lrSpreadSheetExportComponent.ExportImages;
    BandTypes:=lrSpreadSheetExportComponent.BandTypes;
  end
  else
  begin
    FDataGrouping:=ldgLikeReport;
    FDataGroupingChunks:=50;
    FOpenAfterExport:=true;
    FMergeCell:=true;
    FDeleteEmptyRow:=false;
    FExportURL:=false;
    FExportPrintRange:=false;
    FExportImages:=false;
  end;

  FExportMatrix:=TExportMatrix.Create;
  FExportMatrix.MergeCell:=FMergeCell;
  FExportMatrix.DeleteEmptyRow:=FDeleteEmptyRow;

  if AStream is TFileStreamUTF8 then
    FFileName := TFileStreamUTF8(AStream).FileName
  else
    FFileName := '';
end;

destructor TlrSpreadSheetExportFilter.Destroy;
begin
  FExportMatrix.Free;
  if Assigned(FTempMemStreem) then
    FreeAndNil(FTempMemStreem);
  inherited Destroy;
end;

procedure TlrSpreadSheetExportFilter.OnBeginPage;
var
  P: PfrPageInfo;
begin
  inherited OnBeginPage;
  Inc(FCurPage);

  if (FDataGrouping = ldgAllInOnePage) and (FCurPage>1) then
    FExportMatrix.NewPage;
end;

procedure TlrSpreadSheetExportFilter.OnEndPage;
begin
  inherited OnEndPage;
  if FDataGrouping = ldgLikeReport then
    MakeWorksheet;
end;

procedure TlrSpreadSheetExportFilter.OnBeginDoc;
begin
  inherited OnBeginDoc;
//  FTmpTextWidth:=(TempBmp.Canvas.TextWidth('W') + TempBmp.Canvas.TextWidth('i')) div 2;
//  FTmpTextWidth:=TempBmp.Canvas.TextWidth('Wi') / 2;
{  FTmpTextWidth:=TempBmp.Canvas.TextWidth('I');
  FTmpTextHeight:=TempBmp.Canvas.TextHeight('Wg');}
  FTmpTextWidth:=7;
  FTmpTextHeight:=12;
  FWorkbook := TsWorkbook.Create;
  FCurPage:=0;
end;

function GetFormatFromFileName(const AFileName: TFileName;
  out SheetType: TsSpreadsheetFormat): Boolean;
var
  suffix: String;
begin
  Result := true;
  suffix := Lowercase(ExtractFileExt(AFileName));
  case suffix of
    STR_EXCEL_EXTENSION               : SheetType := sfExcel8;
    STR_OOXML_EXCEL_EXTENSION         : SheetType := sfOOXML;
    STR_OPENDOCUMENT_CALC_EXTENSION   : SheetType := sfOpenDocument;
    STR_COMMA_SEPARATED_EXTENSION     : SheetType := sfCSV;
    STR_HTML_EXTENSION, '.htm'        : SheetType := sfHTML;
    STR_WIKITABLE_PIPES_EXTENSION     : SheetType := sfWikiTable_Pipes;
    STR_WIKITABLE_WIKIMEDIA_EXTENSION : SheetType := sfWikiTable_WikiMedia;
    else                                Result := False;
  end;
end;


procedure TlrSpreadSheetExportFilter.OnEndDoc;
var
  S: String;
  sfFileType: TsSpreadsheetFormat;
begin
  inherited OnEndDoc;

  if FDataGrouping = ldgAllInOnePage then
    MakeWorksheet;

  if not GetFormatFromFileName(FFileName, sfFileType) then
    sfFileType:=sfOpenDocument;

  FWorkbook.WriteToStream(Stream, sfFileType);
  FWorkbook.Free;

  FWorkbook:=nil;
end;

procedure TlrSpreadSheetExportFilter.OnExported(x, y: Integer; View: TfrView);
begin
  if not Assigned(View) or not (View.ParentBandType in BandTypes) then exit;  
  if View is TfrCustomBarCodeView then
    ShowBarCode(View as TfrCustomBarCodeView)
  else
  if (View is TfrMemoView) then
    FExportMatrix.ExportObject(View)
  else
  if (View is TfrPictureView) and FExportImages then
    FExportMatrix.ExportObject(View)
end;

initialization
  frRegisterExportFilter(TlrSpreadSheetExportFilter, 'Microsoft Excel (*.xls)', '*.xls');
  frRegisterExportFilter(TlrSpreadSheetExportFilter, 'Microsoft Excel 2007/2010 (*.xlsx)', '*.xlsx');
  frRegisterExportFilter(TlrSpreadSheetExportFilter, 'OpenOffice/LibreOffice (*.ods)', '*.ods');
end.