Solved

Export query output to excel format

Posted on 2001-07-02
7
211 Views
Last Modified: 2010-04-04
Hi Friends

Long time no see!!!.
Could any one help me out my problem

I am opening a query at runtime. I dont want to display the output in report writer. Instead of report writer I need to display the query output in excel

Could anyone help me out with example and code

Regards
Pandian
0
Comment
Question by:Pandian012000
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
Motaz earned 25 total points
Comment Utility
Use this unit:

{----------------------------------------------------------------------}
{  Excel unit : By Motaz Abdel Azeem                                  }
{  Created :  Saturday,  1, April, 2000                                }
{  Updated :  Sunday,   12, November, 2000                             }
{----------------------------------------------------------------------}

unit Excel;

interface

uses Graphics;

function ExcelCreate(SheetName: string; Show: Boolean = True): Variant;
procedure ExcelPut(ExcelObject: Variant; SheetName: string;
  Row, Col: Integer; Data: string; FontName: string = '';
  FontSize: Integer = 0; Color: Integer = clBlack; FontStyles: TFontStyles = []);
procedure ExcelClose(ExcelObject: Variant);

implementation

uses
  ComObj;

{-------------------------------------------------------------------------}
{  ExcelCreate: Open Excel application                                                                                     }
{-------------------------------------------------------------------------}

function ExcelCreate(SheetName: string; Show: Boolean = True): Variant;
const
  xlWBatWorkSheet = -4167;
var
  WorkBook: variant;
  V: Variant;
begin
  V:= CreateOleObject('Excel.Application');
  WorkBook := V.Workbooks.Add(xlWBatWorkSheet);
  WorkBook.WorkSheets[1].Name := SheetName;
  V.Visible := Show;
  Result:= V;
end;

{-------------------------------------------------------------------------}
{  ExcelPut: Put data into cells                                                       }
{-------------------------------------------------------------------------}

procedure ExcelPut(ExcelObject: Variant; SheetName: string;
  Row, Col: Integer; Data: string; FontName: string = '';
  FontSize: Integer = 0; Color: Integer = clBlack; FontStyles: TFontStyles = []);
var
  Sheet: variant;
begin
  Sheet:= ExcelObject.WorkBooks[1].WorkSheets[SheetName];
  Sheet.Cells[Row, Col].Value:= Data;
  Sheet.Cells[Row, Col].Font.Color:= Color;
  if FontName <> '' then
    Sheet.Cells[Row, Col].Font.Name:= FontName;
  if FontSize <> 0 then
    Sheet.Cells[Row, Col].Font.Size:= FontSize;
  if FontSize <> 0 then
    Sheet.Cells[Row, Col].Font.Size:= FontSize;
  Sheet.Cells[Row, Col].Font.Bold:= fsBold in FontStyles;
  Sheet.Cells[Row, Col].Font.Underline:= fsUnderline in FontStyles;
  Sheet.Cells[Row, Col].Font.Italic:= fsItalic in FontStyles;
end;

{-------------------------------------------------------------------------}
{  ExcelClose: Close opened application                                   }
{-------------------------------------------------------------------------}

procedure ExcelClose(ExcelObject: Variant);
begin
  ExcelObject.Quit;
end;

end.
0
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
This is example of how to use previous unit to copy data from your Query to excel sheet.

procedure TForm1.BitBtn1Click(Sender: TObject);
var
 i: Integer;
 Obj: OleVariant;
begin
 Obj:= ExcelCreate('Grid');
 Query1.DisableControls;
 Query1.First;
 for i:= 0 to Query1.Fields.Count - 1 do
   ExcelPut(Obj, 'Grid', 1, i + 1, Query1.Fields[i].FieldName, '', 0, clBlue, [fsBold]);
 with Query1 do
 while not Eof do
 begin
   for i:= 0 to Fields.Count - 1 do
     ExcelPut(Obj, 'Grid', RecNo + 1, i + 1, Fields[i].AsString);
   Next;
 end;
 Query1.EnableControls;
end;

Motaz
0
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
Hi Motaz,
perfect :-)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Pandian012000
Comment Utility
Hi Motaz

Thanks for the code. I tried in my program. It prints only the query fields in the excel sheet not the query records. Am I doing anything wrong. Suggestion most welcome please

Regards

Pandian
0
 

Author Comment

by:Pandian012000
Comment Utility
Hi Motaz

Thank you, It works

regards
Pandian
0
 
LVL 7

Expert Comment

by:Motaz
Comment Utility
Igor, what is your current working e-mail?
0
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
Hi Motaz,
igor@aif.kz
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now