|
By : M. Yadi M.
This article shows how to Create Report with Ms Excel by extracting data from Ms Sql. Here you will learn:
|
||||||||||||||||||||||||||
I will show it by creating an application, but before we go into it, please be sure that you already have Microsoft Excel installed in your PC and you have access to MS SQL Server, and be sure that you have already define table in SQL server database. For this sample I have prepared a table name TTable1 in PRACTICE database, you can have another database and table name if you like.
Let’s go into the application:
1.
Open Delphi application
2.
Create new VCL Project
3.
Put Objects into the Form
*) ConnectionString
value could be different, depend on your Server/PC name and connection method
4.
Add comObj library
5.
Create Event OnClick for Buttion1
6.
Here is the code
|
7.
Lets we test the application by pressing F9 to
run the application
8.
If there is no error, you can see the result as
below picture
Congratulation, now you know how to
create report with Ms Excel by extracting data from MS SQL.
But Wait!!, the format doesn’t look
nice isn’t it? Ok, now I will show you how to have nice format MS Excel.
The steps as follow:
1.
You have to create formatted MS Excel. My format
as below picture but you can have your own format
2.
Save the file into template folder. For example
my template folder is “C:\template\” and the filename is “sample.xls”
3.
Lets modify our code
First add local variable to define
template file name
Then replace code line as
below
Done !, lets Re-Run the application again, if no error the result as
below
Now the format is better. You can play with the format as you want and you can combine your template file with Excel-VBA. for example you can have below format :
It’s easy right? You can have your own MS Excel format.
It’s easy right? You can have your own MS Excel format.