Follow Me

how to generate excel report with php and mysql

Mon, Sep 20, 2010

PHP

Hi Friends,

Recently for one of my project work, I need to generate excel report for customer data using php and mysql. Here I am posting tutorial of generate excel report in easy steps.

generate-excel-report-with-php-and-mysql

Generate-excel-report-with-php-and-mysql

Step 1. Include phpexcel class file along with your config file and make its object with excel file name passed parameter in constructor. Its will display if any error found in next echo statement.


require_once("excelwriter.class.php");

$excel=new ExcelWriter("report.xls");

if($excel==false)
echo $excel->error;

Step 2. Now time to fetch data from mysql database. Before fetch data, call excel header having columns as values of array.


// this will create heading of each column in excel file

$myArr=array("S.No.","Company Name","Email","City","Username","Reg. Date");
$excel->writeLine($myArr);

// now fetch data from database table, there is a new line create each time loop runs

$qry=mysql_query("select * from customer");
if($qry!=false)
{
 $i=1;
 while($res=mysql_fetch_array($qry))
 {
 $myArr=array($i,$res['company_name'],$res['email'],...);
 $excel->writeLine($myArr);
 $i++;
 }
}

Step 3.  Create link to generate excel file.


<a href="javascript:void(0);" onClick="download();">Download Excel Report</a>

and here download function pointing to excel file


<script language="javascript">
function download()
{
 window.location='report.xls';
}
</script>

Thats it. This three steps demonstrate how to generate excel file successfully.

Hope this tuto surely help you to quick develop your export excel report module.

You can download complete source code.

Download excel export report with php and mysql

BE SMART BE HAPPY…

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • blogmarks
  • Design Float
  • DZone
  • MySpace
  • Reddit
  • StumbleUpon
  • Twitter
, , ,

Related Posts:


Recent Posts:

  • Best collection of firefox addons for web designers and developers
  • .htaccess basic features with example
  • how to include an external css and js with javascript dynamically
  • How to preserve line breaks in textarea mysql data
  • how to generate excel report with php and mysql
  • 12 Comments For This Post

    1. han Says:

      hi…

      i’ve try your code but i get an error

      The requested URL /report.xls was not found on this server.

      Can u help me please

    2. Surendar S Says:

      Good Job ! Neatly done.. Keep doing stuffs like this .

    3. admin Says:

      @han This happens when you have removed generated xl sheet from your folder(under www / script folder).

      Just refresh (Ctrl+F5) your browser and it will work fine then.

    4. admin Says:

      @Surendar Thanks

    5. Chinedu Nwankwo Says:

      ………works well ! nice work ! . But i still need help ,I want codes to automatically sum up values entered into spreadsheet cells i.e if i type in 5 into cell A1 and 5 into cell B1 then 10 automatially displays in cell C1 , i know this can be achieved using the formular bar , but can i call up a predetermined template like a report which just fills in values
      How do i go about it ?

      Thankxxx…………….

    6. epcli Says:

      hi,

      i need help. do you know how to show the excel file on the browser instead of downloading it? that way they have the option to check the excel file?

      any suggestion is great.

      thanks in advance!

    7. OseJava Says:

      Thanks,its perfect

    8. Riaz Says:

      how to make the content of excel heading in bold

    9. 3GP Says:

      Hi,

      I new to this coding. I’m designing a web application in PHP which connects to a database in MySQL, this application will generate reports according to the fields that a user choose from diferent tables, there is a way to do this,prefering only by code. (of) Open Source softwares if any..

      I will be happy if you brief in step by step.

    10. brintha Says:

      Sir,

      I used your script and it works fine.Please tell me how to send this excel file through php mail function.Without clicking the download link how to send the file.

    11. deafWim Says:

      how can i get the data from mysql get a border?

    12. Victor Villalobos Says:

      Just great! works fine.

    Leave a Reply