Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

excel report

Status
Not open for further replies.

akaballa123

Technical User
Apr 29, 2008
46
US
hi,

I am trying to generate a mysql report in excel.

this is what I have currently:

Code:

<HTML>
<HEAD>
<TITLE>New Document</TITLE>

Code:
<?php

include("mysqlConnection.php");

 
 function Data(&$str)
 {
   $str = preg_replace("/\t/", "\\t", $str);
   $str = preg_replace("/\n/", "\\n", $str);
  }
    // file name for download
    $filename = "form_data_" . date('Ymd') . ".xls";

   header("Content-Disposition: attachment; filename=\"$filename\"");
   header("Content-Type: application/vnd.ms-excel");
   
   $ticker = false;
   
   $result = @mysql_query('SELECT * FROM sessiontbl') or die('Query failed!');
   
   while(false != ($row = mysql_fetch_assoc($result)))
   {
       if(!$ticker)
       {
         # display field/column names as first row
         echo implode("\t", array_keys($row)) . "\n";
         echo "\n";
         $ticker = true;
        }
        array_walk($row, 'Data');
        echo implode("\t", array_values($row)) . "\n";
   }
    
include("mysqlConnClose.php")
?>
 </HEAD>
</HTML>


The problem is when this report generates in excel the output is shown in the following manner:

sessionID employeeName employeeID trainingName startDate endDate 12 Sriram 0 first 15.10.1988 12.12.1988 13 Sriram 0 first 10.12.2007 12.10.2007 14 Sriram Sampath 20223872 first 12/10/2008 12/25/2008 15 Sriram Sampath 0 first 12/12/1988 12/12/1988 16 Sriram Sampath 0 first 12/12/1988 12/12/1988 17 Sriram Sampath 0 first 12/12/1988 12/12/1988 18 Matt Duniigan 123456 first 12/10/1988 12/10/1988 19 Matt dungoen 123456 first 12/10/1988 12/10/1988 20 james 12321 first 12/10/1988 12/11/1999 21 Sriram Sampath 20223872 first 11/15/2001 11/12/2002 22 Sriram Sampath 20223872 first 12/11/2002 12/12/2003 23 testing 1234565 $value 12/12/1222 12/12/1222 24 testing2 1234321 first 01/02/1988 01/04/1999 25 123 123 G44 11/11/1111 11/11/1111

literally

it is not breaking the line in the correct area. Can someone please help me with this excel report generation. I am pretty new with this. Any information or recommendation would help. Thanks!!

Sriram Sampath
 
Go here
Code:
[URL unfurl="true"]http://www.fpgroups.com/index.php?Target=code[/URL]

There is a MySQL-2-Excel Class you can use. I use it all the time and it is very easy to use.

Good luck!
 
but why bother? just output the data in an html table and open the file in excel. if you're not trying to create editable formulae or similar, i think this is the easiest way.

of course, the new excel format is an open spec so you can always write your own native exporter.
 
As an observation you are telling IE (or whatever) to expect a stream in excel format by giving the file name an extnetion of .xls and a content mime type for excel but you are simply throwing strings at it, it's just getting a bit confused !.
What you might like to try is to give the filename a .csv extention and the mime type should be text/csv. That should at least get you under way for a simple sheet.
 
There a few PHP functions that deal with CSV files like

* str_getcsv()
* explode()
* file()
* pack()
* fputcsv()
* fgetcsv

EXCEL reads and writes CSV files very well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top