douang2297 2011-08-09 04:13
浏览 31

将值导出为Excel文件格式

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['LSUserID']."\">" .$row['ComputerName']."</a></td>";
    echo "<td>" . $row['SerialNumber'] . "</td>";
    echo "<td>" . $row['SystemModel'] . "</td>";
    echo "<td>" . $row['DateTime'] . "</td>";   
  • 写回答

4条回答 默认 最新

  • dongwo1234 2011-08-09 04:27
    关注

    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 
     more gracefully.
    {
        echo "
    ". 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 );
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗