Php 将值导出为Excel文件格式
您好,我在将数组中的值导出到excel时遇到问题,例如,我该怎么做 导出以下内容:Php 将值导出为Excel文件格式,php,excel,Php,Excel,您好,我在将数组中的值导出到excel时遇到问题,例如,我该怎么做 导出以下内容: echo "<table ' border='1' >" echo "<th>ComputerName</th>")); echo "<th>SerialNumber</th>"; echo "<th>SystemModel</th>"; echo "<th>DateTime</t
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['LSUserID']."\">" .$row['ComputerName']."</a></td>";
echo "<td>" . $row['SerialNumber'] . "</td>";
echo "<td>" . $row['SystemModel'] . "</td>";
echo "<td>" . $row['DateTime'] . "</td>";
echo”您需要适当的标题(下面的标题来自PHP文档中的)
在此之后,您的代码应该进行一些修改(Excel可以读取HTML结构):
您可以使用php excel库在excel中读取和写入数据。请查找以下链接:
我发现了一个深受谷歌AppInventor启发的工具,可以在PHP/mySQL环境中创建数据库导出。
检查站点:使用phpBlock,您可以在拖放环境中创建db->excel导出,该环境的灵感来自麻省理工学院的OpenBlock或谷歌的AppInventor。
这意味着不需要用PHP编写任何代码
但导出功能只是php库的一种可能性。您可以将此库用作现有php应用程序的动态扩展,如dyn.formula或calculation。
我希望这比以前的帖子更清楚。将其写入名为spreadsheet.xls的文件中?信不信由你,Excel将以电子表格的形式打开一个HTML表格。csv文件不是更好、更兼容、内置php工具吗?需要更清楚一点的解释,但链接似乎指向一种有效的软件技术。@Andreas一般来说,你应该尝试充实一篇文章,而不仅仅是在文章中有一个链接,特别是从堆栈溢出开始,因为链接垃圾邮件机器人非常流行。
$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).'"');
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>";
// 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 );
}