Re: export data to a ms excel file using php

From: Date: Tue, 18 Dec 2007 19:26:49 +0000
Subject: Re: export data to a ms excel file using php
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-266043@lists.php.net to get a copy of this message
Jim Lucas wrote: > abderrazzak nejeoui wrote: >> can you help me to export data to a ms excel file using php. i tried to >> export them to an html format and change the extension to .xls that's work >> but i have lost the formatting >> >> excel and the navigator doesn't interpret my code in the same why (celles >> are not in the same size) >> > > Ok, so with the examples of others here, here is the shortest example that I came up with that > should get you going. > > <?php > > header("Content-Type: application/vnd.ms-excel"); > header("Expires: 0"); > header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); > > $x = $y = range(0, 12); > > echo '<table>'; > > echo '<tr><td> </td><td>' . > join('</td><td>', $x) . '</td></tr>'; > > foreach ( $x AS $xx ) { > echo "<tr><td>{$xx}</td>"; > foreach ( $y AS $yy ) { > echo > "<td>=sum(".(chr(ord('b')+$yy))."1*a".($xx+2).")</td>"; > } > echo "</tr>"; > } > echo '</table>'; > > ?> > > This will output a 14x14 table. It will calculate the totals for each cell in the actual > spreadsheet once excel loads it. > > If you have any questions, ask away. > Note: as mentioned in one of the articles, you have to change the script name to .xls and have PHP parse the .xls file, otherwise IE will not render correctly. -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare

« previous php.general (#266043) next »