Advertisement
Borrisholt

Dataset Export

Nov 7th, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 26.10 KB | None | 0 0
  1. unit DataSetExport;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Classes, DBTables, DB, Sysutils, Graphics, Forms;
  7.  
  8. type
  9.   TAtributCell = (acHidden, acLocked, acShaded, acBottomBorder, acTopBorder, acRightBorder, acLeftBorder, acLeft, acCenter, acRight, acFill);
  10.   TSetOfAtribut = set of TAtributCell;
  11.  
  12.   TjbFiler = class
  13.   public
  14.     Stream: TStream;
  15.   end;
  16.  
  17.   TjbWriter = class(TjbFiler)
  18.   public
  19.     procedure WriteSingleStr(const S: string);
  20.     procedure WriteStr(const S: string); {req: s shouldn't exceed 64KB}
  21.     procedure WriteByte(B: Byte);
  22.     procedure WriteDouble(D: Double);
  23.     procedure WriteInt(I: Integer);
  24.     procedure WriteWord(W: Word);
  25.   end;
  26.  
  27.   TjbPersistent = class
  28.   public
  29.     opCode: Word; //Important: OpCode<>nil, OpcCode<>OpcodeEOF
  30.     procedure Write(W: TjbWriter); virtual; abstract;
  31.   end;
  32.  
  33.   TDispatcher = class
  34.   private
  35.     StrList: TStringList;
  36.     Writer: TjbWriter;
  37.   protected
  38.     FStream: TStream;
  39.     procedure SetStream(vStream: TStream);
  40.   public
  41.     SLError: TStringList;
  42.     OpcodeEOF: Word;
  43.     procedure Clear;
  44.     procedure RegisterObj(jbPers: TjbPersistent);
  45.     procedure Write;
  46.     constructor Create;
  47.     destructor Destroy; override;
  48.     property Stream: TStream read FStream write SetStream;
  49.   end;
  50.  
  51.   TData = class(TjbPersistent)
  52.   end;
  53.  
  54.   TBOF = class(TData)
  55.     procedure Write(aWriter: TjbWriter); override;
  56.     constructor Create;
  57.   end;
  58.  
  59.   TDimension = class(TData)
  60.     MinSaveRecs,
  61.       MaxSaveRecs,
  62.       MinSaveCols,
  63.       MaxSaveCols: Word;
  64.     procedure Write(aWriter: TjbWriter); override;
  65.     constructor Create;
  66.   end;
  67.  
  68.   TCellClass = class of TCell;
  69.  
  70.   TCell = class(TData)
  71.   protected
  72.     FAtribut: array[0..2] of Byte;
  73.     procedure SetAtribut(Value: TSetOfAtribut);
  74.   public
  75.     Col, Row: Word;
  76.     procedure Write(aWrite: TjbWriter); override;
  77.     property Atribut: TSetOfAtribut write SetAtribut;
  78.     constructor Create; virtual; abstract;
  79.   end;
  80.  
  81.   TBlankCell = class(TCell)
  82.     procedure Write(aWriter: TjbWriter); override;
  83.     constructor Create; override;
  84.   end;
  85.  
  86.   TDoubleCell = class(TCell)
  87.     Value: Double;
  88.     procedure Write(aWriter: TjbWriter); override;
  89.     constructor Create; override;
  90.   end;
  91.  
  92.   TWordCell = class(TCell)
  93.     Value: Word;
  94.     procedure Write(aWriter: TjbWriter); override;
  95.     constructor Create; override;
  96.   end;
  97.  
  98.   TStrCell = class(TCell)
  99.     Value: string;
  100.     procedure Write(aWriter: TjbWriter); override;
  101.     constructor Create; override;
  102.   end;
  103.  
  104.   TjbExportWrapper = class(TDataSet);
  105.   TjbExport = class
  106.   private
  107.     FFileName: string;
  108.     FTable: TjbExportWrapper;
  109.     procedure SetTable(const Value: TjbExportWrapper);
  110.   protected
  111.     TotalCount: Integer;
  112.     Stream: TStream;
  113.  
  114.     function GetStringValue(const aField: TField): string;
  115.  
  116.     procedure DoBeginWriting; virtual;
  117.     procedure DoEndWriting; virtual;
  118.     procedure DoWriteHeader; virtual;
  119.     procedure DoWriteFooter; virtual;
  120.     procedure DoWriteRecord; virtual;
  121.     procedure SaveToStream; virtual;
  122.   public
  123.     constructor Create;
  124.     procedure Execute; virtual;
  125.     property FileName: string read FFileName write FFileName;
  126.     property Table: TjbExportWrapper read FTable write SetTable;
  127.   end;
  128.  
  129.   TjbExportText = class(TjbExport)
  130.   private
  131.     FSeparator: string;
  132.     FBeginString, FEndString: string;
  133.     procedure WriteLn(const S: string);
  134.   protected
  135.     procedure DoWriteHeader; override;
  136.     procedure DoWriteRecord; override;
  137.   public
  138.     constructor Create;
  139.     property Separator: string read FSeparator write FSeparator;
  140.     property BeginString: string read FBeginString write FBeginString;
  141.     property EndString: string read FEndString write FEndString;
  142.   end;
  143.  
  144.   TjbExportHTML = class(TjbExport)
  145.   private
  146.     FHeaderFont: TFont;
  147.     FBodyFont: Tfont;
  148.     FShowGrid: Boolean;
  149.     FHeaderBGColor: TColor;
  150.     FBodyBGColor: TColor;
  151.     procedure WriteLn(S: string);
  152.     procedure SetHeaderFont(const Value: TFont);
  153.     procedure SetBodyFont(const Value: Tfont);
  154.     procedure SetShowGrid(const Value: Boolean);
  155.     procedure SetHeaderBGColor(const Value: TColor);
  156.     procedure SetBodyBGColor(const Value: TColor);
  157.   protected
  158.     procedure DoBeginWriting; override;
  159.     procedure DoEndWriting; override;
  160.     procedure DoWriteHeader; override;
  161.     procedure DoWriteRecord; override;
  162.   public
  163.     constructor Create;
  164.     destructor Destroy; override;
  165.     property HeaderFont: TFont read FHeaderFont write SetHeaderFont;
  166.     property HeaderBGColor: TColor read FHeaderBGColor write SetHeaderBGColor;
  167.     property BodyFont: TFont read FBodyFont write SetBodyFont;
  168.     property BodyBGColor: TColor read FBodyBGColor write SetBodyBGColor;
  169.     property ShowGrid: Boolean read FShowGrid write SetShowGrid;
  170.   end;
  171.  
  172.   TjbExportExcel = class(TjbExport)
  173.   private
  174.     CurrentCol: Integer;
  175.     BOF: TBOF;
  176.     Dimension: TDimension;
  177.     Dispatcher: TDispatcher;
  178.     FShowGrid: Boolean;
  179.  
  180.     function AddCell(vCol, vRow: Word; vAtribut: TSetOfAtribut; CellRef: TCellClass): TCell;
  181.     procedure AddData(aData: TData);
  182.     procedure Clear;
  183.     procedure SetShowGrid(const Value: Boolean);
  184.   protected
  185.     procedure DoBeginWriting; override;
  186.     procedure DoEndWriting; override;
  187.     procedure DoWriteHeader; override;
  188.     procedure DoWriteFooter; override;
  189.     procedure DoWriteRecord; override;
  190.   public
  191.     procedure AddWordCell(vCol, vRow: Word; vAtribut: TSetOfAtribut; aValue: Word);
  192.     procedure AddDoubleCell(vCol, vRow: Word; vAtribut: TSetOfAtribut; aValue: Double);
  193.     procedure AddStrCell(vCol, vRow: Word; vAtribut: TSetOfAtribut; aValue: string);
  194.  
  195.     constructor Create;
  196.     destructor Destroy; override;
  197.     property ShowGrid: Boolean read FShowGrid write SetShowGrid;
  198.   end;
  199. {(*}
  200. type
  201.   TDataSetHelper = class helper for TDataSet
  202.   public
  203.     procedure SaveToText(AFileName: string; const ASeparator: string = ','; const ABeginString: string = '"'; AEndString: string = '"');
  204.     procedure SaveToHTML(const AFileName: string);
  205.     procedure SaveToXLS(const AFileName: string);
  206.   end;
  207. {*)}
  208. procedure SaveToText(aDataset: TDataSet; const AFileName: string; const ASeparator: string = ','; const ABeginString: string = '"'; AEndString: string = '"');
  209. procedure SaveToHTML(aDataset: TDataSet; const AFileName: string);
  210. procedure SaveToXLS(aDataset: TDataSet; const AFileName: string);
  211.  
  212. implementation
  213. uses
  214.   ActiveX, Variants, StrUtils;
  215. const
  216.   {BOF}
  217.   CBOF = $0009;
  218.   BIT_BIFF5 = $0800;
  219.   BIT_BIFF4 = $0400;
  220.   BIT_BIFF3 = $0200;
  221.   BOF_BIFF5 = CBOF or BIT_BIFF5;
  222.   BOF_BIFF4 = CBOF or BIT_BIFF4;
  223.   BOF_BIFF3 = CBOF or BIT_BIFF3;
  224.   {EOF}
  225.   BIFF_EOF = $000A;
  226.   {Document types}
  227.   DOCTYPE_XLS = $0010;
  228.   DOCTYPE_XLC = $0020;
  229.   DOCTYPE_XLM = $0040;
  230.   DOCTYPE_XLW = $0100;
  231.   {Dimensions}
  232.   DIMENSIONS = $0000;
  233.   DIMENSIONS_BIFF4 = DIMENSIONS or BIT_BIFF3;
  234.   DIMENSIONS_BIFF3 = DIMENSIONS or BIT_BIFF3;
  235.  
  236.   { TjbExport }
  237.  
  238. constructor TjbExport.Create;
  239. begin
  240.   inherited;
  241.   FFileName := '';
  242.   FTable := nil;
  243. end;
  244.  
  245. procedure TjbExport.DoBeginWriting;
  246. begin
  247. end;
  248.  
  249. procedure TjbExport.DoEndWriting;
  250. begin
  251. end;
  252.  
  253. procedure TjbExport.DoWriteFooter;
  254. begin
  255. end;
  256.  
  257. procedure TjbExport.DoWriteHeader;
  258. begin
  259. end;
  260.  
  261. procedure TjbExport.DoWriteRecord;
  262. begin
  263. end;
  264.  
  265. procedure TjbExport.Execute;
  266. begin
  267.   if FileName = '' then
  268.     Exception.Create('Filename can not be empty !!!');
  269.  
  270.   Stream := TFileStream.Create(FileName, fmCreate or fmOpenReadWrite);
  271.   try
  272.     SaveToStream;
  273.   finally
  274.     FreeAndNil(Stream);
  275.   end;
  276. end;
  277.  
  278. function TjbExport.GetStringValue(const aField: TField): string;
  279.   function GetValueFromVariant(const KeyValue: Variant): string;
  280.   var
  281.     eTmp: Extended;
  282.   begin
  283.     Result := VarToStr(KeyValue);
  284.  
  285.     case VarType(KeyValue) of //se system.pas, linje 89+
  286.       varInteger, varInt64, varLongWord, varSmallInt, varByte, varWord, varShortInt, varSingle, varDouble, varCurrency:
  287.         begin
  288.           eTmp := KeyValue;
  289.           Result := FloatToStr(eTmp);
  290.         end;
  291.  
  292.       varDate:
  293.         Result := DateToStr(KeyValue);
  294.  
  295.       varBoolean:
  296.         Result := BoolToStr(KeyValue, True);
  297.     end;
  298.   end;
  299.  
  300. begin
  301.   Result := FieldTypeNames[aField.DataType];
  302.  
  303.   case aField.DataType of
  304.     //  ftUnknown: ;
  305.     ftString:
  306.       Result := aField.AsString;
  307.     ftSmallint,
  308.       ftInteger,
  309.       ftWord,
  310.       ftBoolean,
  311.       ftFloat,
  312.       ftCurrency:
  313.       Result := GetValueFromVariant(aField.AsVariant);
  314.     //    ftBCD: ;
  315.     ftDate:
  316.       Result := DateToStr(aField.AsDateTime);
  317.     ftTime:
  318.       Result := TimeToStr(aField.AsDateTime);
  319.     ftDateTime:
  320.       Result := DateTimeToStr(aField.AsDateTime);
  321.     ftBytes:
  322.       Result := GetValueFromVariant(aField.AsVariant);
  323.  
  324.     //    ftVarBytes: ;
  325.     ftAutoInc:
  326.       Result := GetValueFromVariant(aField.AsVariant);
  327.     ftBlob,
  328.       ftMemo:
  329.       Result := aField.AsString;
  330.     //    ftGraphic: ;
  331.     //    ftFmtMemo: ; // 12..18
  332.     //    ftParadoxOle: ;
  333.     //    ftDBaseOle: ;
  334.     //    ftTypedBinary: ;
  335.     //    ftCursor: ;
  336.     //    ftFixedChar: ;
  337.     //    ftWideString: ; // 19..24
  338.     ftLargeint:
  339.       Result := GetValueFromVariant(aField.AsVariant);
  340.     //    ftADT: ;
  341.     //    ftArray: ;
  342.     ftReference:
  343.       Result := TReferenceField(aField).ReferenceTableName;
  344.     //    ftDataSet: ;
  345.     //    ftOraBlob: ;
  346.     //    ftOraClob: ; // 25..31
  347.     ftVariant:
  348.       Result := VarToStr(aField.AsVariant);
  349.     //    ftInterface: ;
  350.     //    ftIDispatch: ;
  351.     ftGuid:
  352.       Result := GUIDToString(TGuidField(aField).AsGuid);
  353.     ftTimeStamp:
  354.       Result := DateTimeToStr(aField.AsDateTime);
  355.     ftFMTBcd:
  356.       Result := aField.AsString; // 32..37
  357.     ftFixedWideChar:
  358.       Result := aField.AsString;
  359.     ftWideMemo:
  360.       Result := aField.AsString;
  361.     ftOraTimeStamp:
  362.       Result := DateTimeToStr(aField.AsDateTime);
  363.     ftOraInterval:
  364.       Result := aField.AsString;
  365.   else
  366.     Result := aField.AsString;      
  367.   end
  368. end;
  369.  
  370. procedure TjbExport.SaveToStream;
  371. begin
  372.   if Assigned(Table) then
  373.     with FTable do
  374.       begin
  375.         DoBeginWriting;
  376.         try
  377.           TotalCount := 0;
  378.           // write header
  379.           DoWriteHeader;
  380.  
  381.           Table.First;
  382.           while not Table.Eof do
  383.             begin
  384.               // processing record
  385.               DoWriteRecord;
  386.               Inc(TotalCount);
  387.  
  388.               Table.Next;
  389.             end;
  390.  
  391.           DoWriteFooter;
  392.         finally
  393.           DoEndWriting;
  394.         end;
  395.       end;
  396. end;
  397.  
  398. procedure TjbExport.SetTable(const Value: TjbExportWrapper);
  399. begin
  400.   FTable := Value;
  401. end;
  402.  
  403. { TjbExportText }
  404.  
  405. constructor TjbExportText.Create;
  406. begin
  407.   Separator := #44;
  408.   FBeginString := #34;
  409.   EndString := #34;
  410. end;
  411.  
  412. procedure TjbExportText.DoWriteHeader;
  413. var
  414.   i: Integer;
  415.   s: string;
  416. begin
  417.   inherited;
  418.   s := '';
  419.  
  420.   for i := 0 to FTable.FieldCount - 1 do
  421.     s := s + FTable.Fields[i].FieldName + Separator;
  422.  
  423.   Delete(s, Length(s) - (Length(Separator) - 1), Length(Separator));
  424.   Writeln(S + sLineBreak);
  425. end;
  426.  
  427. procedure TjbExportText.DoWriteRecord;
  428. var
  429.   s: string;
  430.   Buffer: TStringList;
  431.   i: Integer;
  432.   aField: TField;
  433. begin
  434.   inherited;
  435.   s := '';
  436.  
  437.   Buffer := TStringList.Create;
  438.   Buffer.LineBreak := '';
  439.  
  440.   for I := 0 to FTable.FieldCount - 1 do
  441.     begin
  442.       aField := FTable.Fields[i];
  443.       try
  444.         Buffer.Add(BeginString + GetStringValue(aField) + EndString + IfThen(i = FTable.FieldCount - 1, '', FSeparator));
  445.       except
  446.         Buffer.Add(BeginString + '!!ERROR!!_' + aField.FieldName + EndString + IfThen(i = FTable.FieldCount - 1, '', FSeparator));
  447.       end;
  448.     end;
  449.  
  450.   WriteLn(Buffer.Text + sLineBreak);
  451.   FreeAndNil(Buffer);
  452. end;
  453.  
  454. procedure TjbExportText.WriteLn(const S: string);
  455. begin
  456.   Stream.WriteBuffer(PChar(S)^, Length(S));
  457. end;
  458.  
  459. procedure SaveToXLS(aDataset: TDataSet; const AFileName: string);
  460. begin
  461.   with TjbExportExcel.Create do
  462.     try
  463.       FileName := AFileName;
  464.       Table := TjbExportWrapper(aDataset);
  465.       ShowGrid := False;
  466.       Execute;
  467.     finally
  468.       Free;
  469.     end;
  470. end;
  471.  
  472. procedure SaveToHTML(aDataset: TDataSet; const AFileName: string);
  473. begin
  474.   with TjbExportHTML.Create do
  475.     try
  476.       FileName := AFileName;
  477.       Table := TjbExportWrapper(aDataset);
  478.       Execute;
  479.     finally
  480.       Free;
  481.     end;
  482. end;
  483.  
  484. procedure SaveToText(aDataset: TDataSet; const AFileName: string; const ASeparator: string = ','; const ABeginString: string = '"'; AEndString: string = '"');
  485. begin
  486.   with TjbExportText.Create do
  487.     try
  488.       FileName := AFileName;
  489.       Table := TjbExportWrapper(aDataset);
  490.       Separator := ASeparator;
  491.       BeginString := ABeginString;
  492.       EndString := AEndString;
  493.       Execute;
  494.     finally
  495.       Free;
  496.     end;
  497. end;
  498. { TdxDBGridExportHTML }
  499.  
  500. constructor TjbExportHTML.Create;
  501. begin
  502.   inherited;
  503.   FHeaderFont := TFont.Create;
  504.   FBodyFont := TFont.Create;
  505.  
  506.   FHeaderFont.Name := 'Times New Roman';
  507.   FHeaderFont.Size := 12;
  508.   FBodyFont.Assign(FHeaderFont);
  509.   FShowGrid := True;
  510.   FHeaderBGColor := RGB($A8, $6F, $A8);
  511.   FBodyBGColor := clWhite;
  512. end;
  513.  
  514. destructor TjbExportHTML.Destroy;
  515. begin
  516.   FHeaderFont.Free;
  517.   FBodyFont.Free;
  518.   inherited;
  519. end;
  520.  
  521. procedure TjbExportHTML.DoBeginWriting;
  522. var
  523.   StringList: TStringList;
  524.   aColor: string;
  525. begin
  526.   StringList := TStringList.Create;
  527.   StringList.Add('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">');
  528.   StringList.Add('<HTML>');
  529.   StringList.Add('<HEAD>');
  530.   StringList.Add('<TITLE>');
  531.   StringList.Add(FTable.Name);
  532.   StringList.Add('</Title>');
  533.   StringList.Add('');
  534.   StringList.Add('<STYLE>');
  535.   StringList.Add('<!--');
  536.   StringList.Add('.Header{');
  537.   StringList.Add('  font-family: ' + FHeaderFont.Name + ';');
  538.   StringList.Add('  font-size: ' + IntToStr(FHeaderFont.Size) + ';');
  539.   if FHeaderFont.Color = clNone then
  540.     aColor := '000000'
  541.   else
  542.     aColor := IntToHex(ColorToRGB(FHeaderFont.Color), 6);
  543.  
  544.   StringList.Add('  font-color: #' + aColor + ';');
  545.  
  546.   if FHeaderBGColor = clNone then
  547.     aColor := '000000'
  548.   else
  549.     aColor := IntToHex(ColorToRGB(FHeaderBGColor), 6);
  550.  
  551.   StringList.Add('  background-color : ' + aColor + ';');
  552.   StringList.Add('}');
  553.   StringList.Add('');
  554.   StringList.Add('TD  {');
  555.   StringList.Add('  font-family: ' + FBodyFont.Name + ';');
  556.   StringList.Add('  font-size: ' + IntToStr(FBodyFont.Size) + ';');
  557.  
  558.   if FHeaderFont.Color = clNone then
  559.     aColor := '000000'
  560.   else
  561.     aColor := IntToHex(ColorToRGB(FBodyFont.Color), 6);
  562.  
  563.   StringList.Add('  font-color: ' + aColor + ';');
  564.  
  565.   if FBodyBGColor = clNone then
  566.     aColor := '000000'
  567.   else
  568.     aColor := IntToHex(ColorToRGB(FBodyBGColor), 6);
  569.  
  570.   StringList.Add('  background-color : ' + aColor + ';');
  571.   StringList.Add('}');
  572.   StringList.Add('-->');
  573.   StringList.Add('</STYLE>');
  574.  
  575.   WriteLn(StringList.Text);
  576.   StringList.Free;
  577. end;
  578.  
  579. procedure TjbExportHTML.DoEndWriting;
  580. begin
  581.   WriteLn('    </TABLE>');
  582.   WriteLn('  </TD>');
  583.   WriteLn('  </TR>');
  584.   WriteLn('</TABLE>');
  585.   WriteLn('</Body>');
  586.   WriteLn('</HTML>');
  587. end;
  588.  
  589. procedure TjbExportHTML.DoWriteHeader;
  590. var
  591.   i: Integer;
  592.   StringList: TStringList;
  593.   Border: string;
  594. begin
  595.   if ShowGrid then
  596.     Border := '1'
  597.   else
  598.     Border := '0';
  599.  
  600.   StringList := TStringList.Create;
  601.   StringList.Add('');
  602.   StringList.Add('<BODY BGCOLOR=#C0C0C0>');
  603.   StringList.Add('');
  604.   StringList.Add('<TABLE BORDER=' + Border + ' CELLSPACING=0 CELLPADDING=1 BGCOLOR=#C0C0C0 Width = 100%>');
  605.   StringList.Add('<TR>');
  606.   StringList.Add('  <TD>');
  607.   StringList.Add('    <TABLE BORDER=' + Border + ' CELLSPACING=0 CELLPADDING=1 BGCOLOR=#C0C0C0 Width = 100%>');
  608.   StringList.Add('      <TR VALIGN="TOP" class="Header">');
  609.  
  610.   for i := 0 to FTable.FieldCount - 1 do
  611.     StringList.Add('        <TD NOWRAP  class="Header">' + FTable.Fields[i].FieldName + '</TD>');
  612.  
  613.   StringList.Add('<TR>');
  614.   StringList.Add('');
  615.  
  616.   WriteLn(StringList.Text);
  617.  
  618.   StringList.Free;
  619. end;
  620.  
  621. procedure TjbExportHTML.DoWriteRecord;
  622. var
  623.   s, s1: string;
  624.   i: Integer;
  625. begin
  626.   inherited;
  627.   s := '';
  628.  
  629.   WriteLn('      <TR>');
  630.   for i := 0 to FTable.FieldCount - 1 do
  631.     begin
  632.       S1 := FTable.Fields[i].AsString;
  633.       s := '        <TD NOWRAP>' + S1 + '</TD>';
  634.       WriteLn(s);
  635.     end;
  636.   WriteLn('      </TR>');
  637. end;
  638.  
  639. procedure TjbExportHTML.SetBodyBGColor(const Value: TColor);
  640. begin
  641.   FBodyBGColor := Value;
  642. end;
  643.  
  644. procedure TjbExportHTML.SetBodyFont(const Value: Tfont);
  645. begin
  646.   FBodyFont := Value;
  647. end;
  648.  
  649. procedure TjbExportHTML.SetHeaderBGColor(const Value: TColor);
  650. begin
  651.   FHeaderBGColor := Value;
  652. end;
  653.  
  654. procedure TjbExportHTML.SetHeaderFont(const Value: TFont);
  655. begin
  656.   FHeaderFont := Value;
  657. end;
  658.  
  659. procedure TjbExportHTML.SetShowGrid(const Value: Boolean);
  660. begin
  661.   FShowGrid := Value;
  662. end;
  663.  
  664. procedure TjbExportHTML.WriteLn(S: string);
  665. begin
  666.   S := S + #13#10;
  667.   Stream.WriteBuffer(PChar(S)^, Length(S));
  668. end;                                      
  669.  
  670. { TjbExportExcel }
  671.  
  672. function TjbExportExcel.AddCell(vCol, vRow: Word; vAtribut: TSetOfAtribut; CellRef: TCellClass): TCell;
  673. var
  674.   aCell: TCell;
  675. begin
  676.   aCell := CellRef.Create;
  677.   with aCell do
  678.     begin
  679.       Col := vCol - 1;
  680.       Row := vRow - 1;
  681.       Atribut := vAtribut;
  682.     end;
  683.  
  684.   AddData(aCell);
  685.   Result := aCell;
  686. end;
  687.  
  688. procedure TjbExportExcel.AddData(aData: TData);
  689. begin
  690.   Dispatcher.RegisterObj(aData);
  691. end;
  692.  
  693. procedure TjbExportExcel.AddDoubleCell(vCol, vRow: Word; vAtribut: TSetOfAtribut; aValue: Double);
  694. begin
  695.   with TDoubleCell(AddCell(vCol, vRow, vAtribut, TDoubleCell)) do
  696.     Value := aValue;
  697. end;
  698.  
  699. procedure TjbExportExcel.AddStrCell(vCol, vRow: Word; vAtribut: TSetOfAtribut; aValue: string);
  700. begin
  701.   with TStrCell(AddCell(vCol, vRow, vAtribut, TStrCell)) do
  702.     Value := aValue;
  703. end;
  704.  
  705. procedure TjbExportExcel.AddWordCell(vCol, vRow: Word; vAtribut: TSetOfAtribut; aValue: Word);
  706. begin
  707.   with TWordCell(AddCell(vCol, vRow, vAtribut, TWordCell)) do
  708.     Value := aValue;
  709. end;
  710.  
  711. procedure TjbExportExcel.Clear;
  712. begin
  713.   Dispatcher.Clear;
  714.   BOF := TBOF.Create;
  715.   Dimension := TDimension.Create;
  716.   Dispatcher.RegisterObj(BOF);
  717.   Dispatcher.RegisterObj(Dimension);
  718.   CurrentCol := 1;
  719. end;
  720.  
  721. constructor TjbExportExcel.Create;
  722. begin
  723.   Dispatcher := TDispatcher.Create;
  724.   Dispatcher.OpcodeEOF := BIFF_EOF;
  725.   Clear;
  726. end;
  727.  
  728. destructor TjbExportExcel.Destroy;
  729. begin
  730.   Dispatcher.Free;
  731. end;
  732.  
  733. procedure TjbExportExcel.DoBeginWriting;
  734. begin
  735.  
  736. end;
  737.  
  738. procedure TjbExportExcel.DoEndWriting;
  739. begin
  740.   Dispatcher.Stream := Stream;
  741.   Dispatcher.Write;
  742. end;
  743.  
  744. procedure TjbExportExcel.DoWriteFooter;
  745. begin
  746.   inherited;
  747. end;
  748.  
  749. procedure TjbExportExcel.DoWriteHeader;
  750. var
  751.   i: Integer;
  752.   SetAtribut: TSetOfAtribut;
  753. begin
  754.   SetAtribut := [acShaded, acBottomBorder, acTopBorder, acLeftBorder, acRightBorder, acLeft];
  755.  
  756.   for i := 0 to FTable.FieldCount - 1 do
  757.     AddStrCell(i + 1, CurrentCol, SetAtribut, FTable.Fields[i].FieldName);
  758.  
  759.   inc(CurrentCol);
  760. end;
  761.  
  762. procedure TjbExportExcel.DoWriteRecord;
  763. var
  764.   i: Integer;
  765.   SetAtribut: TSetOfAtribut;
  766. begin
  767.   inc(CurrentCol);
  768.  
  769.   if FShowGrid then
  770.     SetAtribut := [acBottomBorder, acTopBorder, acLeftBorder, acRightBorder, acLeft]
  771.   else
  772.     SetAtribut := [];
  773.  
  774.   for i := 0 to FTable.FieldCount - 1 do
  775.     AddStrCell(i + 1, CurrentCol, SetAtribut, FTable.Fields[i].AsString);
  776. end;
  777.  
  778. procedure TjbExportExcel.SetShowGrid(const Value: Boolean);
  779. begin
  780.   FShowGrid := Value;
  781. end;
  782.  
  783. { TjbWriter }
  784.  
  785. procedure TjbWriter.WriteByte(B: Byte);
  786. begin
  787.   Stream.Write(B, 1);
  788. end;
  789.  
  790. procedure TjbWriter.WriteDouble(D: Double);
  791. begin
  792.   Stream.Write(D, 8);
  793. end;
  794.  
  795. procedure TjbWriter.WriteInt(I: Integer);
  796. begin
  797.   Stream.Write(I, 4);
  798. end;
  799.  
  800. procedure TjbWriter.WriteSingleStr(const S: string);
  801. var
  802.   Tmp: string;
  803. begin
  804.   if s = '' then
  805.     begin
  806.       Tmp := #32;
  807.       Stream.Write(Tmp[1], 1)
  808.     end
  809.   else
  810.     Stream.Write(S[1], Length(S));
  811. end;
  812.  
  813. procedure TjbWriter.WriteStr(const S: string);
  814. {req: s shouldn't exceed 64KB}
  815. var
  816.   Tmp: string;
  817.   Len: Integer;
  818. begin
  819.   if s = '' then
  820.     begin
  821.       Tmp := #32;
  822.       WriteWord(1);
  823.       Stream.Write(Tmp[1], 1)
  824.     end
  825.   else
  826.     begin
  827.       Len := Length(S);
  828.       WriteWord(Len);
  829.       Stream.Write(s[1], Len);
  830.     end;
  831. end;
  832.  
  833. procedure TjbWriter.WriteWord(W: Word);
  834. begin
  835.   Stream.Write(w, 2);
  836. end;
  837.  
  838. { TDispatcher }
  839.  
  840. procedure TDispatcher.Clear;
  841. var
  842.   I: Integer;
  843. begin
  844.   for i := 0 to StrList.Count - 1 do
  845.     TjbPersistent(StrList.Objects[i]).Free;
  846.  
  847.   StrList.Clear;
  848.   SLError.Clear;
  849. end;
  850.  
  851. constructor TDispatcher.Create;
  852. begin
  853.   OpCodeEOF := 999;
  854.   StrList := TStringList.Create;
  855.   Writer := TjbWriter.Create;
  856.   SLError := TStringList.Create;
  857. end;
  858.  
  859. destructor TDispatcher.Destroy;
  860. begin
  861.   Clear;
  862.   StrList.Free;
  863.   Writer.Free;
  864.   SLError.Free;
  865.   inherited;
  866. end;
  867.  
  868. procedure TDispatcher.RegisterObj(jbPers: TjbPersistent);
  869. begin
  870.   StrList.AddObject(IntToStr(jbPers.opCode), jbPers);
  871. end;
  872.  
  873. procedure TDispatcher.SetStream(vStream: TStream);
  874. begin
  875.   FStream := vStream;
  876.   Writer.Stream := FStream;
  877. end;
  878.  
  879. procedure TDispatcher.Write;
  880. var
  881.   i: Integer;
  882.   Pos, Len: Integer;
  883. begin
  884.   for i := 0 to StrList.Count - 1 do
  885.     begin
  886.       Writer.WriteWord(TjbPersistent(StrList.objects[i]).Opcode);
  887.       Writer.WriteWord(0);
  888.       pos := Stream.Position;
  889.       TjbPersistent(StrList.Objects[i]).Write(Writer);
  890.  
  891.       Len := Stream.Position - Pos;
  892.       Stream.Seek(-(Len + 2), soFromCurrent);
  893.       Writer.WriteWord(Len);
  894.       Stream.Seek(Len, soFromCurrent);
  895.     end;
  896.  
  897.   Writer.WriteWord(opCodeEOF);
  898.   Writer.WriteWord(0);
  899. end;
  900.  
  901. { TBOF }
  902.  
  903. constructor TBOF.Create;
  904. begin
  905.   opCOde := BOF_BIFF5;
  906. end;
  907.  
  908. procedure TBOF.Write(aWriter: TjbWriter);
  909. begin
  910.   with aWriter do
  911.     begin
  912.       WriteWord(0);
  913.       WriteWord(DOCTYPE_XLS);
  914.       WriteWord(0);
  915.     end;
  916. end;
  917.  
  918. { TDimension }
  919.  
  920. constructor TDimension.Create;
  921. begin
  922.   opCode := DIMENSIONS;
  923.   MinSaveRecs := 0;
  924.   MaxSaveRecs := 1000;
  925.   MinSaveCols := 0;
  926.   MaxSaveCols := 100;
  927. end;
  928.  
  929. procedure TDimension.Write(aWriter: TjbWriter);
  930. begin
  931.   with aWriter do
  932.     begin
  933.       WriteWord(MinSaveRecs);
  934.       WriteWord(MaxSaveRecs);
  935.       WriteWord(MinSaveCols);
  936.       WriteWord(MaxSaveCols);
  937.     end;
  938. end;
  939.  
  940. { TCell }
  941.  
  942. procedure TCell.SetAtribut(Value: TSetOfAtribut);
  943. var
  944.   i: Integer;
  945. begin
  946.   //reset
  947.   for i := 0 to High(FAtribut) do
  948.     FAtribut[i] := 0;
  949.  
  950.   {
  951.   Byte Offset     Bit   Description                     Contents
  952.   0               7     Cell is not hidden              0b
  953.                         Cell is hidden                  1b
  954.                   6     Cell is not locked              0b
  955.                         Cell is locked                  1b
  956.                   5-0   Reserved, must be 0             000000b
  957.   1               7-6   Font number (4 possible)
  958.                   5-0   Cell format code
  959.   2               7     Cell is not shaded              0b
  960.                         Cell is shaded                  1b
  961.                   6     Cell has no bottom border       0b
  962.                         Cell has a bottom border        1b
  963.                   5     Cell has no top border          0b
  964.                         Cell has a top border           1b
  965.                   4     Cell has no right border        0b
  966.                         Cell has a right border         1b
  967.                   3     Cell has no left border         0b
  968.                         Cell has a left border          1b
  969.                   2-0   Cell alignment code
  970.                              general                    000b
  971.                              left                       001b
  972.                              center                     010b
  973.                              right                      011b
  974.                              fill                       100b
  975.                              Multiplan default align.   111b
  976.   }
  977.  
  978.   //  bit sequence 76543210
  979.  
  980.   if acHidden in Value then //byte 0 bit 7:
  981.     FAtribut[0] := FAtribut[0] + 128;
  982.  
  983.   if acLocked in Value then //byte 0 bit 6:
  984.     FAtribut[0] := FAtribut[0] + 64;
  985.  
  986.   if acShaded in Value then //byte 2 bit 7:
  987.     FAtribut[2] := FAtribut[2] + 128;
  988.  
  989.   if acBottomBorder in Value then //byte 2 bit 6
  990.     FAtribut[2] := FAtribut[2] + 64;
  991.  
  992.   if acTopBorder in Value then //byte 2 bit 5
  993.     FAtribut[2] := FAtribut[2] + 32;
  994.  
  995.   if acRightBorder in Value then //byte 2 bit 4
  996.     FAtribut[2] := FAtribut[2] + 16;
  997.  
  998.   if acLeftBorder in Value then //byte 2 bit 3
  999.     FAtribut[2] := FAtribut[2] + 8;
  1000.  
  1001.   if acLeft in Value then //byte 2 bit 1
  1002.     FAtribut[2] := FAtribut[2] + 1
  1003.   else if acCenter in Value then //byte 2 bit 1
  1004.     FAtribut[2] := FAtribut[2] + 2
  1005.   else if acRight in Value then //byte 2, bit 0 dan bit 1
  1006.     FAtribut[2] := FAtribut[2] + 3;
  1007.  
  1008.   if acFill in Value then //byte 2, bit 0
  1009.     FAtribut[2] := FAtribut[2] + 4;
  1010. end;
  1011.  
  1012. procedure TCell.Write(aWrite: TjbWriter);
  1013. var
  1014.   i: Integer;
  1015. begin
  1016.   with aWrite do
  1017.     begin
  1018.       WriteWord(Row);
  1019.       WriteWord(Col);
  1020.       for i := 0 to 2 do
  1021.         WriteByte(FAtribut[i]);
  1022.     end;
  1023. end;
  1024.  
  1025. { TBlankCell }
  1026.  
  1027. constructor TBlankCell.Create;
  1028. begin
  1029.   opCode := 1;
  1030. end;
  1031.  
  1032. procedure TBlankCell.Write(aWriter: TjbWriter);
  1033. begin
  1034.   inherited;
  1035. end;
  1036.  
  1037. { TDoubleCell }
  1038.  
  1039. constructor TDoubleCell.Create;
  1040. begin
  1041.   opCode := 3;
  1042. end;
  1043.  
  1044. procedure TDoubleCell.Write(aWriter: TjbWriter);
  1045. begin
  1046.   inherited;
  1047.   aWriter.WriteDouble(Value);
  1048. end;
  1049.  
  1050. { TWordCell }
  1051.  
  1052. constructor TWordCell.Create;
  1053. begin
  1054.   opCode := 2;
  1055. end;
  1056.  
  1057. procedure TWordCell.Write(aWriter: TjbWriter);
  1058. begin
  1059.   inherited;
  1060.   aWriter.WriteWord(Value);
  1061. end;
  1062.  
  1063. { TStrCell }
  1064.  
  1065. constructor TStrCell.Create;
  1066. begin
  1067.   opCode := 4;
  1068. end;
  1069.  
  1070. procedure TStrCell.Write(aWriter: TjbWriter);
  1071. begin
  1072.   inherited;
  1073.   aWriter.WriteByte(Length(Value));
  1074.   aWriter.WriteSingleStr(Value);
  1075. end;
  1076.  
  1077. { TDataSetHelper }
  1078.  
  1079. procedure TDataSetHelper.SaveToHTML(const AFileName: string);
  1080. begin
  1081.   DataSetExport.SaveToHTML(Self, AFileName);
  1082. end;
  1083.  
  1084. procedure TDataSetHelper.SaveToText(AFileName: string; const ASeparator, ABeginString: string; AEndString: string);
  1085. begin
  1086.   DataSetExport.SaveToText(Self, AFileName, ASeparator, ABeginString, AEndString);
  1087. end;
  1088.  
  1089. procedure TDataSetHelper.SaveToXLS(const AFileName: string);
  1090. begin
  1091.   DataSetExport.SaveToXLS(Self, AFileName);
  1092. end;
  1093.  
  1094. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement