1. Tech

{
Article: From ADO Query to HTML - DB/20

http://delphi.about.com/library/weekly/aa112701a.htm

Chapter twenty of the free Delphi Database Course for beginners. 
How to export your data to HTML using Delphi and ADO. 
This is the first step in publishing your database on the 
Internet - see how to create a static HTML page from an ADO query.

}

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, Grids, DBGrids, ADODB, OleCtrls, SHDocVw, ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    WebBrowser1: TWebBrowser;
    Panel2: TPanel;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Edit1: TEdit;
    Button1: TButton;
    Panel3: TPanel;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var ConStr: widestring;
begin
ConStr := 'Provider=Microsoft.Jet.OLEDB.4.0;'+
          'Data Source=C:\!gajba\About\aboutdelphi.mdb;'+
          'Persist Security Info=False';

DBGrid1.DataSource := DataSource1;
DataSource1.DataSet := ADOQuery1;
ADOQuery1.Connection := ADOConnection1;
ADOConnection1.ConnectionString := ConStr;
ADOConnection1.LoginPrompt:=False;

Edit1.Text:='SELECT * FROM [tablename]';
Memo1.Text:='';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
ADOQuery1.Close;
ADOConnection1.Close;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  shtml     : widestring;
  htmlfile  : TextFile;
  i         : integer;
  AvailableFields: set of  TFieldType;
begin
AvailableFields:=[ftWideString, ftDate,  ftFloat];
//open query
ADOQuery1.SQL.Text:=Edit1.Text;
ADOQuery1.Open;

// --> create a html page

//html header
  shtml:= '<html> <head> <title>';
  shtml:= shtml + Edit1.Text;
  shtml:= shtml + '</title></head>' + #13#10;
  shtml:= shtml + '<body>' + #13#10;
  shtml:= shtml + 'Table created from query: <i>' + Edit1.Text + '</i>' + #13#10;

//table header
  shtml:= shtml + '<table border="1" width="100%">' + #13#10;
  shtml:= shtml + '<tr>' + #13#10;
  for i:=0 to AdoQuery1.FieldCount-1 do
  begin
    if ADOQuery1.Fields[i].DataType
       in AvailableFields  then
    begin
      shtml:= shtml + '<td>';
      shtml:= shtml + '<b>' + ADOQuery1.Fields[i].DisplayName + '</b>';
      shtml:= shtml + '</td>' + #13#10;
    end;
  end;{for}
  shtml:= shtml + '</tr>' + #13#10;

//table body
  while not adoquery1.Eof do
  begin
    shtml:= shtml + '<tr>' + #13#10;
    for i:=0 to AdoQuery1.FieldCount-1 do
    begin
      if ADOQuery1.Fields[i].DataType
         in AvailableFields then
      begin
        shtml:= shtml + '<td>';
        shtml:= shtml + ADOQuery1.Fields[i].AsString;
        shtml:= shtml + '</td>' + #13#10;
      end;
    end;{for}
    shtml:= shtml + '</tr>' + #13#10;
    ADOQuery1.Next;
  end;{while}
  shtml:= shtml + '</table>' + #13#10;

  shtml:= shtml + '</body></html>';

// --> assign to memo
  Memo1.Text := shtml;

// --> save in an htm file
 AssignFile(htmlfile, ChangeFileExt(Application.ExeName,'.htm'));
 Rewrite(htmlfile);
 WriteLn(htmlfile, shtml);
 CloseFile(htmlfile);


// --> browse to the file
 WebBrowser1.Navigate(ChangeFileExt(Application.ExeName,'.htm'));
end;

end.



FORM1.DFM

Select Form1, Select View As Text, Paste the text into Editor, Select View As Form. object Form1: TForm1 Left = 257 Top = 190 Width = 378 Height = 287 ActiveControl = Edit1 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate OnDestroy = FormDestroy PixelsPerInch = 96 TextHeight = 13 object Panel1: TPanel Left = 0 Top = 121 Width = 370 Height = 60 Align = alClient BorderWidth = 3 Caption = 'Panel1' TabOrder = 0 object WebBrowser1: TWebBrowser Left = 4 Top = 4 Width = 362 Height = 52 Align = alClient TabOrder = 0 ControlData = { 4C0000006A250000600500000000000000000000000000000000000000000000 000000004C000000000000000000000001000000E0D057007335CF11AE690800 2B2E126208000000000000004C0000000114020000000000C000000000000046 8000000000000000000000000000000000000000000000000000000000000000 00000000000000000100000000000000000000000000000000000000} end end object Panel2: TPanel Left = 0 Top = 0 Width = 370 Height = 121 Align = alTop Caption = 'Panel2' TabOrder = 1 DesignSize = ( 370 121) object DBGrid1: TDBGrid Left = 1 Top = 40 Width = 368 Height = 80 Align = alBottom TabOrder = 0 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clWindowText TitleFont.Height = -11 TitleFont.Name = 'MS Sans Serif' TitleFont.Style = [] end object Edit1: TEdit Left = 8 Top = 8 Width = 283 Height = 21 Anchors = [akLeft, akTop, akRight] TabOrder = 1 Text = 'Edit1' end object Button1: TButton Left = 301 Top = 8 Width = 62 Height = 25 Anchors = [akTop, akRight] Caption = 'Button1' TabOrder = 2 OnClick = Button1Click end end object Panel3: TPanel Left = 0 Top = 181 Width = 370 Height = 79 Align = alBottom Caption = 'Panel3' TabOrder = 2 object Memo1: TMemo Left = 1 Top = 1 Width = 368 Height = 77 Align = alClient Lines.Strings = ( 'Memo1') ScrollBars = ssBoth TabOrder = 0 end end object ADOConnection1: TADOConnection Left = 160 Top = 8 end object ADOQuery1: TADOQuery Parameters = <> Left = 192 Top = 8 end object DataSource1: TDataSource Left = 128 Top = 8 end end { ******************************************** Zarko Gajic About.com Guide to Delphi Programming http://delphi.about.com email: delphi@aboutguide.com free newsletter: http://delphi.about.com/library/blnewsletter.htm forum: http://forums.about.com/ab-delphi/start/ ******************************************** }

©2014 About.com. All rights reserved.