Export values to Excel file format
Hello I having trouble exporting values from an array to excel for example how would I export the following:
echo "<table ' border='1' >"
echo "<th>ComputerName</th>"));
echo "<th>SerialNumber</th>";
echo "<th>SystemModel</th>";
echo "<th>DateTime</th>";
echo "<th>Software</th>";
echo "<th>Hardware</th>";
echo "<th>Support</th>";
echo "<th>Delete</th>";
echo "</tr>";
$alt_color = 0;
while($row = mysql_fetch_array($result))
{
error_reporting (E_ALL ^ E_NOTICE);
//foreach( $array_values as $value )
$bgc = ( $alt_color % 2 ? 'Dark' : 'Light' );
echo "<tr class=".$bgc.">";
//echo "<td><a href=\"update.php?LSUserID=" .$row['LSUserID']."\">" .$row['LSUserID']."</a></td>";
echo "<td><a href=\"update.php?LSUserID=" .$row['LSUse开发者_JAVA技巧rID']."\">" .$row['ComputerName']."</a></td>";
echo "<td>" . $row['SerialNumber'] . "</td>";
echo "<td>" . $row['SystemModel'] . "</td>";
echo "<td>" . $row['DateTime'] . "</td>";
You'll need the appropriate headers (the ones below are from an example in the PHP docs)
$export = "my_name.xls";
header('Pragma: public');
/////////////////////////////////////////////////////////////
// prevent caching....
/////////////////////////////////////////////////////////////
// Date in the past sets the value to already have been expired.
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
header ("Pragma: no-cache");
header("Expires: 0");
/////////////////////////////////////////////////////////////
// end of prevent caching....
/////////////////////////////////////////////////////////////
header('Content-Transfer-Encoding: none');
// This should work for IE & Opera
header('Content-Type: application/vnd.ms-excel;');
// This should work for the rest
header("Content-type: application/x-msexcel");
header('Content-Disposition: attachment; filename="'.basename($export).'"');
After that, your code should work with a couple of modifications (Excel can read HTML structures):
error_reporting (E_ALL ^ E_NOTICE); // you shouldn't have that in the loop.
// you actually should put this first
echo "<table >"
// the rest of your table opening code.
$alt_color = 0;
while($row = mysql_fetch_array($result))
{
echo "<tr>";
// the rest of your columns.
echo "</tr>";
}
echo "</table>";
If that does not work, for some reason, then you can create a CSV, but you need to worry about escaping it:
// add all of the headers.
echo '"ComputerName","SerialNumber","SystemModel",'; //etc for all columns.
// row makes sure that there is only one set of values.
$row = mysql_fetch_row($result);
if( !$row ) die(); // no value found. exit.
echo getCSVRow( $row );
do // do...while lets us handle the \n more gracefully.
{
echo "\n". getCSVRow( $row );
} while ($row = mysql_fetch_row($result));
function getCSVRow( array $row )
{
$ret = "";
foreach( $row as $val )
$ret .= '"' . escapeCSV( $val ) . '",';
return sustr( $ret, 0, strlen( $ret ) ); // clear the tailing comma
}
function escapeCSV( $str )
{
return str_replace( '"', '\"', $str );
}
You can use php-excel library for reading and writing data in excel. Find below link:
- http://phpexcel.codeplex.com/
- http://code.google.com/p/php-excel/
- http://www.ibm.com/developerworks/opensource/library/os-phpexcel/
I have found a tool heavily inspired from Google's AppInventor to create database exports in an PHP/mySQL enviroment. inspect the site: http://www.freegroup.de/software/phpBlocks/demo.html
With phpBlock you can create your db->excel export in a drag drop environment which is inspired from OpenBlock from MIT or AppInventor from Google. This means it is not necessary to code anything in PHP.
But the export feature is only one possibility of the php-lib. You can use this lib as a dynamic extension to your existing PHP application. Like dyn. formula or calculation. I hope this more clear than the posting before.
精彩评论