如果是直接导出到EXCEL的话,依目前的技术能力似乎还不能做到。连大名鼎鼎的OpenOffice对EXCEL的支持也不是很好。
不过有种投机取巧的方法可以实现,就是从数据库中取出数据后,生成一个表格,样式自己定义好,然后让浏览器识别为下载excel。以下只是一个示例:<?php
// 转载请注明phpteam
$title = "数据库名:test, 数据表:test, 备份日期:" . date("Y-m-d H:i:s");$conn = @mysql_connect("localhost", "root", "") or die("不能连接数据库");
@mysql_select_db("test", $conn);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo '<table border="1" cellspacing="2" cellpadding="2" width="50%" align="center">';
// 输出标题
echo '<tr bgcolor="#cccccc"><td colspan="3" align="center">' . $title . '</td></tr>';$query = "select * from test";
$result = mysql_query($query) or die(mysql_error());
$fields = mysql_num_fields($result);
// 输出字段名
echo '<tr bgcolor="blue">';
for($i = 0; $i < $fields; $i++) {
    echo '<td>' . mysql_field_name($result, $i) . '</td>';
}
echo '</tr>';
// 输出内容
while($row = mysql_fetch_row($result)) {
    echo '<tr>';
    for($i = 0; $i<$fields; $i++) {
        echo '<td>' . $row[$i] . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>

解决方案 »

  1.   

    其实这个问题应该这样问:用ASP把数据库导出为excel文件,并且能导出格式及背景、线条等
    如果行,则用php就行
      

  2.   

    我今天找到一个网上,可以这样做的,但不知道怎么做的。http://www.time-assistant.com/tastandard/hlogin.phpusername:joesenpassword:joesen进入admin菜单下,有一个XLS按钮,导出为excel,导到本地,你看一下就知道了。
      

  3.   

    哈哈,托你的福,我已经知道怎么解决了,你把下面着段代码存为.xls文件看看,知道该怎么做了吧?
      <html xmlns:o="urn:schemas-microsoft-com:office:office"
            xmlns:x="urn:schemas-microsoft-com:office:excel"
            xmlns="http://www.w3.org/TR/REC-html40">
      <head>
            <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
            <meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
            <!--[if gte mso 9]><xml>
            <x:ExcelWorkbook>
            <x:ExcelWorksheets>
                    <x:ExcelWorksheet>
                    <x:Name></x:Name>
                    <x:WorksheetOptions>
                            <x:DisplayGridlines/>
                    </x:WorksheetOptions>
                    </x:ExcelWorksheet>
            </x:ExcelWorksheets>
            </x:ExcelWorkbook>
            </xml><![endif]-->  </head><table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr><td align="center" class="big6" >Users (except the ones disabled)</td></tr>
     
    <tr><td class="greyborder">
      <table border="1" align="center" width="100%" cellpadding="1" cellspacing="1">
      <tr align="center">
          <td class="TableTopHeader" nowrap>
          Login   </td>
       <td class="TableTopHeader" nowrap>
          Name   </td>
       <td class="TableTopHeader" width="3%" nowrap>
          EMail   </td>
       <td class="TableTopHeader" nowrap>
        Department   </td>
       <td class="TableTopHeader" nowrap>
        Position   </td>
       <td class="TableTopHeader" nowrap width="1%">
         Level   </td>
       <td class="TableTopHeader">Address</td>
       <td class="TableTopHeader">Phone</td>
    </tr>
        <tr  class="even">
         
          <td class="mtlist">&nbsp;joesen</td>
          <td class="mtlist">&nbsp;hong joesen</td>
          <td class="mtlist" >[email protected]</td>
          <td class="mtlist">&nbsp;Software developments</td>
          <td class="mtlist">&nbsp;System Administrator</td>
          <td class="mtlist">&nbsp;Admin</td>
         
          
          <td class="mtlist">&nbsp;</td>
          <td class="mtlist">&nbsp;</td>
      </tr>
      </table>
    </td></tr>  
    </table>
      

  4.   

    结贴吧!
    这就是 phpteam(我忍住不哭) 提供的方法不错,不错。有长见识了!
      

  5.   

    zh_yuandc(中原大帝)贴出的就是从http://www.time-assistant.com/tastandard/hlogin.php
    下载下来的.xls文件,你用文本编辑器打开就是那个样子。你用Excel打开就可以进行编辑。我理解他是一个Excel可识别处理的xml文档,不是真正的Excel文档。但这已经很好了。