Недавно добавленные исходники

•  DeLiKaTeS Tetris (Тетрис)  136

•  TDictionary Custom Sort  3 318

•  Fast Watermark Sources  3 065

•  3D Designer  4 825

•  Sik Screen Capture  3 321

•  Patch Maker  3 536

•  Айболит (remote control)  3 637

•  ListBox Drag & Drop  2 996

•  Доска для игры Реверси  81 578

•  Графические эффекты  3 927

•  Рисование по маске  3 232

•  Перетаскивание изображений  2 613

•  Canvas Drawing  2 735

•  Рисование Луны  2 561

•  Поворот изображения  2 166

•  Рисование стержней  2 161

•  Paint on Shape  1 564

•  Генератор кроссвордов  2 227

•  Головоломка Paletto  1 764

•  Теорема Монжа об окружностях  2 218

•  Пазл Numbrix  1 682

•  Заборы и коммивояжеры  2 052

•  Игра HIP  1 279

•  Игра Go (Го)  1 225

•  Симулятор лифта  1 471

•  Программа укладки плитки  1 214

•  Генератор лабиринта  1 542

•  Проверка числового ввода  1 353

•  HEX View  1 490

•  Физический маятник  1 355

 
скрыть


Delphi FAQ - Часто задаваемые вопросы

| Базы данных | Графика и Игры | Интернет и Сети | Компоненты и Классы | Мультимедиа |
| ОС и Железо | Программа и Интерфейс | Рабочий стол | Синтаксис | Технологии | Файловая система |



Delphi Sources

Экспортировать TDBGrid в Excel без использования OLE



Оформил: DeeCo

{ 
  Exporting a DBGrid to excel without OLE 

  I develop software and about 95% of my work deals with databases. 
  I enjoied the advantages of using Microsoft Excel in my projects 
  in order to make reports but recently I decided to convert myself 
  to the free OpenOffice suite. 
  I faced with the problem of exporting data to Excel without having 
  Office installed on my computer. 
  The first solution was to create directly an Excel format compatible file: 
  this solution is about 50 times faster than the OLE solution but there 
  is a problem: the output file is not compatible with OpenOffice. 
  I wanted a solution which was compatible with each "DataSet"; 
  at the same time I wanted to export only the dataset data present in 
  a DBGrid and not all the "DataSet". 
  Finally I obtained this solution which satisfied my requirements. 
  I hope that it will be usefull for you too. 

  First of all you must import the ADOX type library 
  which will be used to create the Excel file and its 
  internal structure: in the Delphi IDE: 

  1)Project->Import Type Library: 
  2)Select "Microsoft ADO Ext. for DDL and Security" 
  3)Uncheck "Generate component wrapper" at the bottom 
  4)Rename the class names (TTable, TColumn, TIndex, TKey, TGroup, TUser, TCatalog) in 
    (TXTable, TXColumn, TXIndex, TXKey, TXGroup, TXUser, TXCatalog) 
    in order to avoid conflicts with the already present TTable component. 
  5)Select the Unit dir name and press "Create Unit". 
    It will be created a file named AOX_TLB. 
    Include ADOX_TLB in the "uses" directive inside the file in which you want 
    to use ADOX functionality. 

  That is all. Let's go now with the implementation: 
}

 unit DBGridExportToExcel;

 interface

 uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   ExtCtrls, StdCtrls, ComCtrls, DB, IniFiles, Buttons, dbgrids, ADOX_TLB, ADODB;


 type TScrollEvents = class
        BeforeScroll_Event: TDataSetNotifyEvent;
        AfterScroll_Event: TDataSetNotifyEvent;
        AutoCalcFields_Property: Boolean;
   end;

 procedure DisableDependencies(DataSet: TDataSet; var ScrollEvents: TScrollEvents);
 procedure EnableDependencies(DataSet: TDataSet; ScrollEvents: TScrollEvents);
 procedure DBGridToExcelADO(DBGrid: TDBGrid; FileName: string; SheetName: string);


 implementation

 //Support procedures: I made that in order to increase speed in 
//the process of scanning large amounts 
//of records in a dataset 

//we make a call to the "DisableControls" procedure and then disable the "BeforeScroll" and 
//"AfterScroll" events and the "AutoCalcFields" property. 
procedure DisableDependencies(DataSet: TDataSet; var ScrollEvents: TScrollEvents);
 begin
      with DataSet do
           begin
                DisableControls;
                ScrollEvents := TScrollEvents.Create();
                with ScrollEvents do
                     begin
                          BeforeScroll_Event := BeforeScroll;
                          AfterScroll_Event := AfterScroll;
                          AutoCalcFields_Property := AutoCalcFields;
                          BeforeScroll := nil;
                          AfterScroll := nil;
                          AutoCalcFields := False;
                     end;
           end;
 end;

 //we make a call to the "EnableControls" procedure and then restore 
// the "BeforeScroll" and "AfterScroll" events and the "AutoCalcFields" property. 
procedure EnableDependencies(DataSet: TDataSet; ScrollEvents: TScrollEvents);
 begin
      with DataSet do
           begin
                EnableControls;
                with ScrollEvents do
                     begin
                          BeforeScroll := BeforeScroll_Event;
                          AfterScroll := AfterScroll_Event;
                          AutoCalcFields := AutoCalcFields_Property;
                     end;
           end;
 end;

 //This is the procedure which make the work: 

procedure DBGridToExcelADO(DBGrid: TDBGrid; FileName: string; SheetName: string);
 var
   cat: _Catalog;
   tbl: _Table;
   col: _Column;
   i: integer;
   ADOConnection: TADOConnection;
   ADOQuery: TADOQuery;
   ScrollEvents: TScrollEvents;
   SavePlace: TBookmark;
 begin
   // 
  //WorkBook creation (database) 
  cat := CoCatalog.Create;
   cat._Set_ActiveConnection('Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0');
   //WorkSheet creation (table) 
  tbl := CoTable.Create;
   tbl.Set_Name(SheetName);
   //Columns creation (fields) 
  DBGrid.DataSource.DataSet.First;
   with DBGrid.Columns do
     begin
       for i := 0 to Count - 1 do
         if Items[i].Visible then
         begin
           col := nil;
           col := CoColumn.Create;
           with col do
             begin
               Set_Name(Items[i].Title.Caption);
               Set_Type_(adVarWChar);
             end;
           //add column to table 
          tbl.Columns.Append(col, adVarWChar, 20);
         end;
     end;
   //add table to database 
  cat.Tables.Append(tbl);

   col := nil;
   tbl := nil;
   cat := nil;

   //exporting 
  ADOConnection := TADOConnection.Create(nil);
   ADOConnection.LoginPrompt := False;
   ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0';
   ADOQuery := TADOQuery.Create(nil);
   ADOQuery.Connection := ADOConnection;
   ADOQuery.SQL.Text := 'Select * from [' + SheetName + '$]';
   ADOQuery.Open;


   DisableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);
   SavePlace := DBGrid.DataSource.DataSet.GetBookmark;
   try
   with DBGrid.DataSource.DataSet do
     begin
       First;
       while not Eof do
         begin
           ADOQuery.Append;
           with DBGrid.Columns do
             begin
               ADOQuery.Edit;
               for i := 0 to Count - 1 do
                 if Items[i].Visible then
                   begin
                     ADOQuery.FieldByName(Items[i].Title.Caption).AsString := FieldByName(Items[i].FieldName).AsString;
                   end;
               ADOQuery.Post;
             end;
           Next;
         end;
     end;

   finally
   DBGrid.DataSource.DataSet.GotoBookmark(SavePlace);
   DBGrid.DataSource.DataSet.FreeBookmark(SavePlace);
   EnableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);

   ADOQuery.Close;
   ADOConnection.Close;

   ADOQuery.Free;
   ADOConnection.Free;

   end;

 end;

 end.




Похожие по теме исходники

Экспорт баз данных в Excel

Transfer Tables Excel-Access

Excel Films Lister

Work with Word and Excel

 

База данных без BDE

БД без BDE

Кривая Безье

Bezier Curves (кривые Безье)

 

Splay Line (Безье)

Сплайн на 8 точек (Безье)

Text Bezier Curve (Безье)

Кривые Безье на OpenGL

 

Пример использования DBGrid

Win Console

Molecula (3D молекула)

Molecule (молекула)

 

Console FTP

CMD OLE

DeParole

Console SmartEngine

 

Close Console on Event

Console Task Manager




Copyright © 2004-2024 "Delphi Sources" by BrokenByte Software. Delphi World FAQ

Группа ВКонтакте