Jump to content

SMARAM

uniGUI Subscriber
  • Posts

    50
  • Joined

  • Last visited

  • Days Won

    3

Everything posted by SMARAM

  1. The same code in C++ Builder, if someone needs it. TStringList *lstSessions = new TStringList(); TUniGUISessions *S = UniServerModule()->SessionManager->Sessions; int nr = S->SessionList->Count; S->Lock(); // Using Lock on session manager should be used with extreme care. // We should keep this "Lock" active as short as we can. Session Manager stops working until lock is released. // Excessive use of lock/unlock or keeping the lock active for a long period will make uniGUI server slow/unresponsive. try { // Here we do a fast copy of list to another array for later use. for (int i=0; i<nr; i++) { TUniGUISession *U = (TUniGUISession*)S->SessionList->Items[i]; lst->Add("SessionId: " + U->SessionId); lst->Add("RemoteIP: " + U->RemoteIP); lst->Add("LastTimeStamp: " + U->LastTimeStamp); } } __finally { S->Unlock(); // ... and finally don't forget to release lock! }
  2. Hi Sherzod. Version:1.0.0.1416 When the unidbgrid has a column locked, this code doens't work UniSession.AddJS(UniDBGrid1.JSName + '.headerCt.move(1,5);' + UniDBGrid1.JSName + '.view.refresh();'); This error is displayed.. I was trying to find the error in the ext-all file and the error occurred here, because it is empty Without a locked column, this works normally.
  3. This doesn't work when dbgrid has a locked column. Any idea?
  4. Which language are you exporting? Is it not ansi characters?
  5. Hi, your problem is linking the file into your project. 1) Did you put the UExportExcel into uses of frmcontact? 2) Did you add UExportExcel into your project?
  6. I already use ClientEvents but I would like to encapsulate my method. Which method ClientEvents is loaded? Is there a way to override it?
  7. JSAddListener('afterrender', GetJSInputMaskDate);
  8. Hi, I want to put a javascript encapsulate on a customDateTimePicker. function afterrender(sender, eOpts) { $("#"+sender.inputEl.id).inputmask("99/99/9999",{placeholder:"__/__/____"}); I'm trying to add the method on WebCreate but it give me error 'There is no Default JavaScript Object. [ : TUniSmDateTimePicker]'. If I put it in LoadCompleted, it doesn't work. Is there any other method to add the listener that could be encapsulate?
  9. Sherzod, did you try unzip the file?
  10. Can't download it too. File may be corrupted or deleted in server.
  11. I can't right now. This is the current version on my company.
  12. I found a solution that is not so elegant. 1) set dbgrid with Visible = false. 2) On UniDBGrid AfterLoad put this code: UniSession.AddJS('ajaxRequest(frameName.FramePanel,"UniDBGrid1",[])'); 3) On UniFrame AjaxEvent put this code: if (EventName = 'UniDBGrid1') then begin UniDBGrid1.Visible := true; end;
  13. I'm having the same problem. To reproduce the scenario, follow the steps below. My unigui version: 1.0.0.1416. 1) Create an empty project. 2) Put a UniSimplePanel with Align = alClient 3) Create an Unigui Frame 4) Put some simplepanels into the frame as Align = alTop and a unidbgrid below. 5) The dbgrid must be binded with a datasource and dataset to reproduce the scenario 6) Increase the height so that it will be greater than your screen 7) On FormCreate of MainForm, use the code below to create the frame. var Frame1: TUniFrame1; Frame1 = TUniFrame1.Create(Self); Frame1.Align := alClient; Frame1.Parent := UniSimplePanel1; 7) Run your code and see that your frame will be positioned on the dbgrid. Ps: It will only occurs when your dataset has result.
  14. Yes, I did. Thank you.
  15. Thanks for replying. What I was trying to do is writing directly html inside the cell to create a listview using the unidbgrid. What I did was writing directly in my dataset mixing html and values.
  16. Which example are you talking about?
  17. Thanks for replying. I thought I could do it on BeforeLogin because it could easier to implement.
  18. I found a way to get my request on MainModule.HandleRequest but as my webapp is SynchronousMode, I can't change the Handled to true in BeforeLogin. I need something to call my javascript on BeforeLogin and get the result there so I can change Handled to true.
  19. Hi, I'm trying integrate my unigui app with another webapp where the user is already logged. I created an javascript to authenticate and I'm calling in BeforeLogin like this. UniSession.AddJS('authenticate()'). I do the authentication in js function but I don't know how to send the request to MainModule.HandleRequest. Any help?
  20. I think the nginx is not the problem. I need to change the src root to the nginx location. Example: /smaramweb.dll/ -> /amteste/smaramweb.dll
  21. Hi, I'm using nginx to redirect my unigui applications on IIS, but I need help to setting up. When nginx redirects an application to location, the files sources don't work. I've alredy tried to change ExtRoot and UniRoot, but without success. nginx conf upstream amteste { server localhost:91; } server { listen 80; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host:$server_port; location = /amteste { return 301 /amteste/; } location /amteste/ { proxy_pass http://amteste/; } } Is it possible? Any idea?
  22. Hi Mika, I just updated my Unigui version to Ext 6.5 and was having some problems too. Maybe this post can help you. But when you update ext4 to ext6, you may have some problems with javascript like "sender.addClass" that changed to "sender.addCls". So I created a converter to change inside my dfm based on files "DE.txt" and "PARA.txt". The converter was necessary since dfm has string truncated inside. Conversor_EXTJS4_EXTJS6.zip
  23. SMARAM

    Erase Cached CSS

    I found a solution that I found on another topic. The files are different since my version (Ext 6.5). 1) On directory ext\build\classic, copy directory theme "theme-neptune" (that's my custom theme) as "theme-neptune2" for example. 2) Inside this directory, rename file "theme-neptune.js" to "theme-neptune2.js" 3) Inside directory ext\build\classic\theme-neptune2\resources, rename file "theme-neptune-all.css" to "theme-neptune2-all.css" 4) On your project, go to MainModule and change property Theme to "neptune2" That's all. No need to tell your users to delete cache on browser.
  24. For people who is having problems with download the zip file. UExportExcel.pas //============================================================================= // TDataSet to Excel without OLE or Excel required // Mike Heydon Dec 2002 // Adapted to Unigui with TUniDBGrid // Mauricio Naozuka - 26/04/2018 - naozuka@gmail.com //============================================================================= unit UExportExcel; // Example // // Add uses UExportExcel // //procedure TMainForm.UniButton1Click(Sender: TObject); //var url, filename, reportname : String; // exportExcel: TDataSetToExcel; // i: integer; //begin // reportname := 'ExcelReport'; // url := UniServerModule.LocalCacheURL+name+'.xls'; // filename := UniServerModule.NewCacheFileUrl(false, 'xls', reportname, '', url); // // exportExcel := TDataSetToExcel.Create; // exportExcel.WriteFile(filename, UniDBGrid1); // FreeAndNil(exportExcel); // UniSession.SendFile(filename, reportname+'.xls'); //end; interface uses Windows, SysUtils, DB, Math, uniBasicGrid, uniDBGrid; type // TDataSetToExcel TDataSetToExcel = class(TObject) protected procedure WriteToken(AToken: word; ALength: word); procedure WriteFont(const AFontName: Ansistring; AFontHeight, AAttribute: word); procedure WriteFormat(const AFormatStr: Ansistring); private FRow: word; FFieldCount: integer; FDataFile: file; FFileName: string; public constructor Create; function WriteFile(const AFileName: string; const AGrid: TUniDBGrid): boolean; end; //----------------------------------------------------------------------------- implementation const // XL Tokens XL_DIM = $00; XL_BOF = $09; XL_EOF = $0A; XL_DOCUMENT = $10; XL_FORMAT = $1E; XL_COLWIDTH = $24; XL_FONT = $31; // XL Cell Types XL_INTEGER = $02; XL_DOUBLE = $03; XL_STRING = $04; // XL Cell Formats XL_INTFORMAT = $81; XL_DBLFORMAT = $82; XL_XDTFORMAT = $83; XL_DTEFORMAT = $84; XL_TMEFORMAT = $85; XL_HEADBOLD = $40; XL_HEADSHADE = $F8; // ======================== // Create the class // ======================== constructor TDataSetToExcel.Create; begin FFieldCount := 0; end; // ==================================== // Write a Token Descripton Header // ==================================== procedure TDataSetToExcel.WriteToken(AToken: word; ALength: word); var aTOKBuffer: array[0..1] of word; begin aTOKBuffer[0] := AToken; aTOKBuffer[1] := ALength; Blockwrite(FDataFile, aTOKBuffer, SizeOf(aTOKBuffer)); end; // ==================================== // Write the font information // ==================================== procedure TDataSetToExcel.WriteFont(const AFontName: ansistring; AFontHeight, AAttribute: word); var iLen: byte; begin AFontHeight := AFontHeight * 20; WriteToken(XL_FONT, 5 + length(AFontName)); BlockWrite(FDataFile, AFontHeight, 2); BlockWrite(FDataFile, AAttribute, 2); iLen := length(AFontName); BlockWrite(FDataFile, iLen, 1); BlockWrite(FDataFile, AFontName[1], iLen); end; // ==================================== // Write the format information // ==================================== procedure TDataSetToExcel.WriteFormat(const AFormatStr: ansistring); var iLen: byte; begin WriteToken(XL_FORMAT, 1 + length(AFormatStr)); iLen := length(AFormatStr); BlockWrite(FDataFile, iLen, 1); BlockWrite(FDataFile, AFormatStr[1], iLen); end; // ==================================== // Write the XL file from data set // ==================================== function TDataSetToExcel.WriteFile(const AFilename:String; const AGrid: TUniDBGrid): boolean; var bRetvar: boolean; aDOCBuffer: array[0..1] of word; aDIMBuffer: array[0..3] of word; aAttributes: array[0..2] of byte; i: integer; iColNum, iDataLen: byte; sStrData: string; fDblData: double; wWidth: word; sStrBytes: TBytes; begin if not Assigned(AGrid) then raise Exception.Create('There is no Grid is vinculated.'); if not Assigned(AGrid.DataSource) then raise Exception.Create('There is no DataSource is vinculated to Grid ' + AGrid.Name); if not Assigned(AGrid.DataSource.DataSet) then raise Exception.Create('There is no DataSet is vinculated to DataSource ' + AGrid.DataSource.Name); bRetvar := true; FRow := 0; FillChar(aAttributes, SizeOf(aAttributes), 0); FFileName := ChangeFileExt(AFilename, '.xls'); AssignFile(FDataFile, FFileName); try Rewrite(FDataFile, 1); // Beginning of File WriteToken(XL_BOF, 4); aDOCBuffer[0] := 0; aDOCBuffer[1] := XL_DOCUMENT; Blockwrite(FDataFile, aDOCBuffer, SizeOf(aDOCBuffer)); // Font Table WriteFont('Arial', 10, 0); WriteFont('Arial', 10, 1); //WriteFont('Courier New', 11, 0); // Column widths iColNum := 0; for i := 0 to AGrid.Columns.Count-1 do begin if not AGrid.Columns[i].Visible then continue; if AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DisplayWidth + 1 > Length(AGrid.Columns[i].Title.Caption) then begin wWidth := (AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DisplayWidth + 1) * 256; end else begin wWidth := (Length(AGrid.Columns[i].Title.Caption) + 1) * 256; end; // Limitar o tamanho da coluna if wWidth > 80*256 then wWidth := 80*256; // if AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DataType = ftDateTime then // inc(wWidth, 100); // if AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DataType = ftDate then // inc(wWidth, 1050); // if AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DataType = ftTime then // inc(wWidth, 100); WriteToken(XL_COLWIDTH, 4); BlockWrite(FDataFile, iColNum, 1); BlockWrite(FDataFile, iColNum, 1); BlockWrite(FDataFile, wWidth, 2); Inc(iColNum); end; FFieldCount := iColNum; // Column Formats WriteFormat('Geral'); WriteFormat('0'); WriteFormat('#.##0,0000'); WriteFormat('dd/mm/aaaa hh:mm:ss'); WriteFormat('dd/mm/aaaa'); WriteFormat('hh:mm:ss'); // Dimensions WriteToken(XL_DIM, 8); aDIMBuffer[0] := 0; aDIMBuffer[1] := Min(AGrid.DataSource.DataSet.RecordCount, $FFFF); aDIMBuffer[2] := 0; aDIMBuffer[3] := Min(FFieldCount - 1, $FFFF); Blockwrite(FDataFile, aDIMBuffer, SizeOf(aDIMBuffer)); // Column Headers iColNum := 0; for i := 0 to AGrid.Columns.Count-1 do begin if not AGrid.Columns[i].Visible then continue; // sStrData := FDataSet.Fields[i].DisplayName; sStrBytes := TEncoding.ANSI.GetBytes(AGrid.Columns[i].Title.Caption); iDataLen := length(sStrBytes); WriteToken(XL_STRING, iDataLen + 8); WriteToken(FRow, iColNum); aAttributes[1] := XL_HEADBOLD; //aAttributes[2] := XL_HEADSHADE; BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes)); BlockWrite(FDataFile, iDataLen, SizeOf(iDataLen)); if iDataLen > 0 then BlockWrite(FDataFile, sStrBytes[0], iDataLen); aAttributes[2] := 0; Inc(iColNum); end; try AGrid.DataSource.DataSet.DisableControls; AGrid.DataSource.DataSet.First; // Data Rows while not AGrid.DataSource.DataSet.Eof do begin inc(FRow); iColNum := 0; for i := 0 to AGrid.Columns.Count-1 do begin if not AGrid.Columns[i].Visible then continue; case AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DataType of ftBoolean, ftWideString, ftFixedChar, ftString: begin try // sStrData := FDataSet.Fields[i].AsString; sStrBytes:=TEncoding.ANSI.GetBytes(AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsString); iDataLen := length(sStrBytes); WriteToken(XL_STRING, iDataLen + 8); WriteToken(FRow, iColNum); aAttributes[1] := 0; BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes)); BlockWrite(FDataFile, iDataLen, SizeOf(iDataLen)); if iDataLen > 0 then BlockWrite(FDataFile, sStrBytes[0], iDataLen); except on E: Exception do //ShowMessage(E.Message); raise Exception.Create('Erro Converter: ' + E.Message); end; end; ftAutoInc, ftSmallInt, ftInteger, ftWord, ftLargeInt: begin try fDblData := AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsFloat; iDataLen := SizeOf(fDblData); WriteToken(XL_DOUBLE, 15); WriteToken(FRow, iColNum); aAttributes[1] := XL_INTFORMAT; BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes)); BlockWrite(FDataFile, fDblData, iDatalen); except on E: Exception do //ShowMessage(E.Message); raise Exception.Create('Erro Converter Inteiro: ' + E.Message); end; end; ftFloat, ftCurrency, ftBcd, ftFMTBcd: begin try fDblData := AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsFloat; iDataLen := SizeOf(fDblData); WriteToken(XL_DOUBLE, 15); WriteToken(FRow, iColNum); aAttributes[1] := XL_DBLFORMAT; BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes)); BlockWrite(FDataFile, fDblData, iDatalen); except on E: Exception do //ShowMessage(E.Message); raise Exception.Create('Erro Converter Float: ' + E.Message); end; end; ftDateTime: begin try if not AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).IsNull then begin fDblData := AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsFloat; iDataLen := SizeOf(fDblData); WriteToken(XL_DOUBLE, 15); WriteToken(FRow, iColNum); aAttributes[1] := XL_XDTFORMAT; BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes)); BlockWrite(FDataFile, fDblData, iDatalen); end; except on E: Exception do //ShowMessage(E.Message); raise Exception.Create('Erro Converter DateTime: ' + E.Message); end; end; ftDate: begin try if not AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).IsNull then begin fDblData := AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsFloat; iDataLen := SizeOf(fDblData); WriteToken(XL_DOUBLE, 15); WriteToken(FRow, iColNum); aAttributes[1] := XL_DTEFORMAT; BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes)); BlockWrite(FDataFile, fDblData, iDatalen); end; except on E: Exception do //ShowMessage(E.Message); raise Exception.Create('Erro Converter Date: ' + E.Message); end; end; ftTime: begin try if not AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).IsNull then begin fDblData := AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsFloat; iDataLen := SizeOf(fDblData); WriteToken(XL_DOUBLE, 15); WriteToken(FRow, iColNum); aAttributes[1] := XL_TMEFORMAT; BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes)); BlockWrite(FDataFile, fDblData, iDatalen); end; except on E: Exception do //ShowMessage(E.Message); raise Exception.Create('Erro Converter Time: ' + E.Message); end; end; ftMemo: begin // Does not print memo end; else raise Exception.Create('Tipo [' + AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).ClassName + '] do campo [' + AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).FieldName + '] não foi tratado.'); end; Inc(iColNum); end; // end of for AGrid.DataSource.DataSet.Next; end; // end of while finally AGrid.DataSource.DataSet.EnableControls; AGrid.DataSource.DataSet.First; end; // End of File WriteToken(XL_EOF, 0); CloseFile(FDataFile); except bRetvar := false; end; Result := bRetvar; end; end.
  25. //=============================================================================
    // TDataSet to Excel without OLE or Excel required
    // Mike Heydon Dec 2002
    
    // Adapted to Unigui with TUniDBGrid
    // Mauricio Naozuka - 26/04/2018 - naozuka@gmail.com
    //=============================================================================
    
    unit UExportExcel;
    
    //  Example
    //
    //  Add uses UExportExcel
    //
    //procedure TMainForm.UniButton1Click(Sender: TObject);
    //var url, filename, reportname : String;
    //    exportExcel: TDataSetToExcel;
    //    i: integer;
    //begin
    //	reportname := 'ExcelReport';
    //	url := UniServerModule.LocalCacheURL+name+'.xls';
    //	filename := UniServerModule.NewCacheFileUrl(false, 'xls', reportname, '', url);
    //
    //	exportExcel := TDataSetToExcel.Create;
    //  exportExcel.WriteFile(filename, UniDBGrid1);
    //  FreeAndNil(exportExcel);
    //  UniSession.SendFile(filename, reportname+'.xls');
    //end;
    
    interface
    
    uses Windows, SysUtils, DB, Math, uniBasicGrid, uniDBGrid;
    
    type
      // TDataSetToExcel
      TDataSetToExcel = class(TObject)
      protected
        procedure WriteToken(AToken: word; ALength: word);
        procedure WriteFont(const AFontName: Ansistring; AFontHeight,
          AAttribute: word);
        procedure WriteFormat(const AFormatStr: Ansistring);
      private
        FRow: word;
        FFieldCount: integer;
        FDataFile: file;
        FFileName: string;
      public
        constructor Create;
        function WriteFile(const AFileName: string; const AGrid: TUniDBGrid): boolean;
      end;
    
    //-----------------------------------------------------------------------------
    implementation
    
    const
      // XL Tokens
      XL_DIM = $00;
      XL_BOF = $09;
      XL_EOF = $0A;
      XL_DOCUMENT = $10;
      XL_FORMAT = $1E;
      XL_COLWIDTH = $24;
      XL_FONT = $31;
    
      // XL Cell Types
      XL_INTEGER = $02;
      XL_DOUBLE = $03;
      XL_STRING = $04;
    
      // XL Cell Formats
      XL_INTFORMAT = $81;
      XL_DBLFORMAT = $82;
      XL_XDTFORMAT = $83;
      XL_DTEFORMAT = $84;
      XL_TMEFORMAT = $85;
      XL_HEADBOLD = $40;
      XL_HEADSHADE = $F8;
    
      // ========================
      // Create the class
      // ========================
    
    constructor TDataSetToExcel.Create;
    begin
      FFieldCount := 0;
    end;
    
    // ====================================
    // Write a Token Descripton Header
    // ====================================
    
    procedure TDataSetToExcel.WriteToken(AToken: word; ALength: word);
    var
      aTOKBuffer: array[0..1] of word;
    begin
      aTOKBuffer[0] := AToken;
      aTOKBuffer[1] := ALength;
      Blockwrite(FDataFile, aTOKBuffer, SizeOf(aTOKBuffer));
    end;
    
    // ====================================
    // Write the font information
    // ====================================
    
    procedure TDataSetToExcel.WriteFont(const AFontName: ansistring;
      AFontHeight, AAttribute: word);
    var
      iLen: byte;
    begin
      AFontHeight := AFontHeight * 20;
      WriteToken(XL_FONT, 5 + length(AFontName));
      BlockWrite(FDataFile, AFontHeight, 2);
      BlockWrite(FDataFile, AAttribute, 2);
      iLen := length(AFontName);
      BlockWrite(FDataFile, iLen, 1);
      BlockWrite(FDataFile, AFontName[1], iLen);
    end;
    
    // ====================================
    // Write the format information
    // ====================================
    
    procedure TDataSetToExcel.WriteFormat(const AFormatStr: ansistring);
    var
      iLen: byte;
    begin
      WriteToken(XL_FORMAT, 1 + length(AFormatStr));
      iLen := length(AFormatStr);
      BlockWrite(FDataFile, iLen, 1);
      BlockWrite(FDataFile, AFormatStr[1], iLen);
    end;
    
    // ====================================
    // Write the XL file from data set
    // ====================================
    
    function TDataSetToExcel.WriteFile(const AFilename:String; const AGrid: TUniDBGrid): boolean;
    var
      bRetvar: boolean;
      aDOCBuffer: array[0..1] of word;
      aDIMBuffer: array[0..3] of word;
      aAttributes: array[0..2] of byte;
      i: integer;
      iColNum,
      iDataLen: byte;
      sStrData: string;
      fDblData: double;
      wWidth: word;
      sStrBytes: TBytes;
    begin
      if not Assigned(AGrid) then
        raise Exception.Create('There is no Grid is vinculated.');
    
      if not Assigned(AGrid.DataSource) then
        raise Exception.Create('There is no DataSource is vinculated to Grid ' + AGrid.Name);
    
      if not Assigned(AGrid.DataSource.DataSet) then
        raise Exception.Create('There is no DataSet is vinculated to DataSource ' + AGrid.DataSource.Name);
    
      bRetvar := true;
      FRow := 0;
      FillChar(aAttributes, SizeOf(aAttributes), 0);
      FFileName := ChangeFileExt(AFilename, '.xls');
      AssignFile(FDataFile, FFileName);
    
      try
        Rewrite(FDataFile, 1);
        // Beginning of File
        WriteToken(XL_BOF, 4);
        aDOCBuffer[0] := 0;
        aDOCBuffer[1] := XL_DOCUMENT;
        Blockwrite(FDataFile, aDOCBuffer, SizeOf(aDOCBuffer));
    
        // Font Table
        WriteFont('Arial', 10, 0);
        WriteFont('Arial', 10, 1);
        //WriteFont('Courier New', 11, 0);
    
        // Column widths
        iColNum := 0;
        for i := 0 to AGrid.Columns.Count-1 do
        begin
          if not AGrid.Columns[i].Visible then
            continue;
    
          if AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DisplayWidth + 1 >
             Length(AGrid.Columns[i].Title.Caption) then
          begin
            wWidth := (AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DisplayWidth + 1) * 256;
          end
          else
          begin
            wWidth := (Length(AGrid.Columns[i].Title.Caption) + 1) * 256;
          end;
    
          // Limitar o tamanho da coluna
          if wWidth > 80*256 then
            wWidth := 80*256;
    
    //      if AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DataType = ftDateTime then
    //        inc(wWidth, 100);
    //      if AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DataType = ftDate then
    //        inc(wWidth, 1050);
    //      if AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DataType = ftTime then
    //        inc(wWidth, 100);
    
          WriteToken(XL_COLWIDTH, 4);
    
          BlockWrite(FDataFile, iColNum, 1);
          BlockWrite(FDataFile, iColNum, 1);
          BlockWrite(FDataFile, wWidth, 2);
          Inc(iColNum);
        end;
    
        FFieldCount := iColNum;
    
        // Column Formats
        WriteFormat('Geral');
        WriteFormat('0');
        WriteFormat('#.##0,0000');
        WriteFormat('dd/mm/aaaa hh:mm:ss');
        WriteFormat('dd/mm/aaaa');
        WriteFormat('hh:mm:ss');
    
        // Dimensions
        WriteToken(XL_DIM, 8);
        aDIMBuffer[0] := 0;
        aDIMBuffer[1] := Min(AGrid.DataSource.DataSet.RecordCount, $FFFF);
        aDIMBuffer[2] := 0;
        aDIMBuffer[3] := Min(FFieldCount - 1, $FFFF);
        Blockwrite(FDataFile, aDIMBuffer, SizeOf(aDIMBuffer));
    
        // Column Headers
        iColNum := 0;
        for i := 0 to AGrid.Columns.Count-1 do
        begin
          if not AGrid.Columns[i].Visible then
            continue;
    
          //      sStrData := FDataSet.Fields[i].DisplayName;
          sStrBytes := TEncoding.ANSI.GetBytes(AGrid.Columns[i].Title.Caption);
          iDataLen := length(sStrBytes);
          WriteToken(XL_STRING, iDataLen + 8);
          WriteToken(FRow, iColNum);
          aAttributes[1] := XL_HEADBOLD;
          //aAttributes[2] := XL_HEADSHADE;
          BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes));
          BlockWrite(FDataFile, iDataLen, SizeOf(iDataLen));
          if iDataLen > 0 then
            BlockWrite(FDataFile, sStrBytes[0], iDataLen);
          aAttributes[2] := 0;
          Inc(iColNum);
        end;
    
        try
          AGrid.DataSource.DataSet.DisableControls;
          AGrid.DataSource.DataSet.First;
    
          // Data Rows
          while not AGrid.DataSource.DataSet.Eof do
          begin
            inc(FRow);
            iColNum := 0;
    
            for i := 0 to AGrid.Columns.Count-1 do
            begin
              if not AGrid.Columns[i].Visible then
                continue;
    
              case AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).DataType of
                  ftBoolean,
                  ftWideString,
                  ftFixedChar,
                  ftString:
                  begin
                    try
                      //              sStrData := FDataSet.Fields[i].AsString;
    
                      sStrBytes:=TEncoding.ANSI.GetBytes(AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsString);
                      iDataLen := length(sStrBytes);
                      WriteToken(XL_STRING, iDataLen + 8);
                      WriteToken(FRow, iColNum);
                      aAttributes[1] := 0;
                      BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes));
                      BlockWrite(FDataFile, iDataLen, SizeOf(iDataLen));
                      if iDataLen > 0 then
                        BlockWrite(FDataFile, sStrBytes[0], iDataLen);
                    except on E: Exception do
                      //ShowMessage(E.Message);
                      raise Exception.Create('Erro Converter: ' + E.Message);
                    end;
                  end;
    
                  ftAutoInc,
                  ftSmallInt,
                  ftInteger,
                  ftWord,
                  ftLargeInt:
                  begin
                    try
                    fDblData := AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsFloat;
                    iDataLen := SizeOf(fDblData);
                    WriteToken(XL_DOUBLE, 15);
                    WriteToken(FRow, iColNum);
                    aAttributes[1] := XL_INTFORMAT;
                    BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes));
                    BlockWrite(FDataFile, fDblData, iDatalen);
                    except on E: Exception do
                      //ShowMessage(E.Message);
                      raise Exception.Create('Erro Converter Inteiro: ' + E.Message);
                    end;
                  end;
    
                  ftFloat,
                  ftCurrency,
                  ftBcd,
                  ftFMTBcd:
                  begin
                    try
                    fDblData := AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsFloat;
                    iDataLen := SizeOf(fDblData);
                    WriteToken(XL_DOUBLE, 15);
                    WriteToken(FRow, iColNum);
                    aAttributes[1] := XL_DBLFORMAT;
                    BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes));
                    BlockWrite(FDataFile, fDblData, iDatalen);
                    except on E: Exception do
                      //ShowMessage(E.Message);
                      raise Exception.Create('Erro Converter Float: ' + E.Message);
                    end;
                  end;
    
                ftDateTime:
                  begin
                    try
                      if not AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).IsNull then
                      begin
                        fDblData := AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsFloat;
                        iDataLen := SizeOf(fDblData);
                        WriteToken(XL_DOUBLE, 15);
                        WriteToken(FRow, iColNum);
                        aAttributes[1] := XL_XDTFORMAT;
                        BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes));
                        BlockWrite(FDataFile, fDblData, iDatalen);
                      end;
                    except on E: Exception do
                      //ShowMessage(E.Message);
                      raise Exception.Create('Erro Converter DateTime: ' + E.Message);
                    end;
                  end;
    
                ftDate:
                  begin
                    try
                      if not AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).IsNull then
                      begin
                        fDblData := AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsFloat;
                        iDataLen := SizeOf(fDblData);
                        WriteToken(XL_DOUBLE, 15);
                        WriteToken(FRow, iColNum);
                        aAttributes[1] := XL_DTEFORMAT;
                        BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes));
                        BlockWrite(FDataFile, fDblData, iDatalen);
                      end;
                    except on E: Exception do
                      //ShowMessage(E.Message);
                      raise Exception.Create('Erro Converter Date: ' + E.Message);
                    end;
                  end;
    
                ftTime:
                  begin
                    try
                      if not AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).IsNull then
                      begin
                        fDblData := AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).AsFloat;
                        iDataLen := SizeOf(fDblData);
                        WriteToken(XL_DOUBLE, 15);
                        WriteToken(FRow, iColNum);
                        aAttributes[1] := XL_TMEFORMAT;
                        BlockWrite(FDataFile, aAttributes, SizeOf(aAttributes));
                        BlockWrite(FDataFile, fDblData, iDatalen);
                      end;
                    except on E: Exception do
                      //ShowMessage(E.Message);
                      raise Exception.Create('Erro Converter Time: ' + E.Message);
                    end;
                  end;
    
                ftMemo:
                  begin
                    // Does not print memo
                  end;
    
              else raise Exception.Create('Tipo [' + AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).ClassName + '] do campo [' +
                                          AGrid.DataSource.DataSet.FieldByName(AGrid.Columns[i].FieldName).FieldName + '] não foi tratado.');
    
              end;
    
              Inc(iColNum);
            end; // end of for
    
            AGrid.DataSource.DataSet.Next;
          end; // end of while
    
        finally
          AGrid.DataSource.DataSet.EnableControls;
          AGrid.DataSource.DataSet.First;
        end;
    
        // End of File
        WriteToken(XL_EOF, 0);
        CloseFile(FDataFile);
      except
        bRetvar := false;
      end;
    
      Result := bRetvar;
    end;
    
    end.

     

×
×
  • Create New...