File: le_e_spreadsheet.pas

package info (click to toggle)
lazarus 1.6.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 172,444 kB
  • ctags: 124,173
  • sloc: pascal: 1,528,777; xml: 260,232; sh: 3,008; java: 603; makefile: 512; perl: 297; sql: 222; ansic: 137
file content (434 lines) | stat: -rw-r--r-- 13,848 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
{
  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}

unit le_e_spreadsheet;

{$mode objfpc}{$H+}

interface

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

type

  { TlrSpreadSheetExportFilter }

  TlrSpreadSheetExportFilter = class(TfrExportFilter)
  private
    FDataGrouping: TlreDataGrouping;
    FDataGroupingChunks: integer;
    FDeleteEmptyRow: boolean;
    FExportMatrix:TExportMatrix;
    FMergeCell: boolean;
    FOpenAfterExport: boolean;
    FWorkbook:TsWorkbook;
    FWorksheet:TsWorksheet;
    FFileName:string;
    FCurPage:integer;
{    FTmpTextWidth: Integer;
    FTmpTextHeight: Integer;}
    FTmpTextWidth: Double;
    FTmpTextHeight: Double;
    procedure ExportColWidth;
    procedure ExportRowHight;
    //procedure ExportData;
    procedure ExportData1;

    procedure MakeWorksheet;
  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 ShowBarCode(View: TfrBarCodeView; x, y, h, w: 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;
  end;

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

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;

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.ExportData;
var
  R:TExportObject;
  y: Integer;
  x: Integer;
  scFrm:TsCellBorders;

begin
  for y:=0 to FExportMatrix.RowCount do
    for x:=0 to FExportMatrix.ColumnCount do
    begin
      R:=FExportMatrix.Objects[X, Y];
      if Assigned(R) then
      begin
        if R.Text<>'' then
        begin
          FWorksheet.WriteUTF8Text(Y, X, TrimRight(R.Texts.Text));
          if R.Angle <> 0 then
            FWorksheet.WriteTextRotation(Y, X, rt90DegreeCounterClockwiseRotation);
          FWorksheet.WriteVertAlignment(Y, X, ssLayout[R.Layout]);
          FWorksheet.WriteWordwrap(Y, X, R.WordWrap);
        end;
        FWorksheet.WriteBackgroundColor(Y, X, R.FillColor);

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

        if Assigned(R.Font) then
          FWorksheet.WriteFont(Y, X, R.Font.Name,  R.Font.Size, sftofs(R.Font), R.Font.Color, fpNormal);

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

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

        if scFrm <> [] then
          FWorksheet.WriteBorders(Y, X, scFrm);
      end;
    end;
end;
}
procedure TlrSpreadSheetExportFilter.ExportData1;
var
  R: Integer;
  Row: TExportRow;
  C: Integer;
  Cel: TExportObject;
  X: Integer;
  Y: Integer;
  scFrm:TsCellBorders;
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.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);
        end;
        FWorksheet.WriteBackgroundColor(Y, X, Cel.FillColor);

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

        if Assigned(Cel.Font) 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);
      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;
  //ExportData;
  ExportData1;
  FWorksheet:=nil;
  FExportMatrix.Clear;
end;

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

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

  leSpreadsheetParamsForm:=TleSpreadsheetParamsForm.Create(Application);
  leSpreadsheetParamsForm.RadioButton4.Checked:=FDataGrouping = ldgLikeReport;
  leSpreadsheetParamsForm.RadioButton5.Checked:=FDataGrouping = ldgAllInOnePage;
  leSpreadsheetParamsForm.RadioButton6.Checked:=FDataGrouping = ldgChunks;
  leSpreadsheetParamsForm.SpinEdit1.Value:=FDataGroupingChunks;
  leSpreadsheetParamsForm.CheckBox4.Checked:=FOpenAfterExport;
  leSpreadsheetParamsForm.CheckBox2.Checked:=FMergeCell;
  leSpreadsheetParamsForm.CheckBox6.Checked:=FDeleteEmptyRow;


  Result:=leSpreadsheetParamsForm.ShowModal = mrOk;
  if Result then
  begin
    if leSpreadsheetParamsForm.RadioButton4.Checked then
      FDataGrouping:=ldgLikeReport
    else
    if leSpreadsheetParamsForm.RadioButton5.Checked then
      FDataGrouping:=ldgAllInOnePage
    else
    begin
      FDataGrouping:=ldgChunks;
      FDataGroupingChunks:=leSpreadsheetParamsForm.SpinEdit1.Value;
    end;
    FOpenAfterExport:=leSpreadsheetParamsForm.CheckBox4.Checked;
    FMergeCell      := leSpreadsheetParamsForm.CheckBox2.Checked;
    FDeleteEmptyRow := leSpreadsheetParamsForm.CheckBox6.Checked;

    FExportMatrix.MergeCell:=FMergeCell;
    FExportMatrix.DeleteEmptyRow:=FDeleteEmptyRow;
  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;
  end
  else
  begin
    FDataGrouping:=ldgLikeReport;
    FDataGroupingChunks:=50;
    FOpenAfterExport:=true;
    FMergeCell:=true;
    FDeleteEmptyRow:=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;
  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;

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) then exit;
  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.