当前位置:Linux教程 - Php - 生成excel的文件

生成excel的文件

<?
header
("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=test.xls");


$sql = "select ";
if (
$HTTP_POST_VARS["C1"] == "ON" ) $sql .= "hdid, ";
if (
$C2 == "ON" ) $sql .= "location, ";
if (
$C3 == "ON" ) $sql .= "getip, ";
if (
$C4 == "ON" ) $sql .= "type, ";
if (
$C5 == "ON" ) $sql .= "pcusername, ";
if (
$C6 == "ON" ) $sql .= "usergroup, ";
if (
$C7 == "ON" ) $sql .= "ram, ";
if (
$C8 == "ON" ) $sql .= "cpu, ";
if (
$C9 == "ON" ) $sql .= "disk, ";
if (
$C10 == "ON" ) $sql .= "lanmac, ";
if (
$C11 == "ON" ) $sql .= "monitor, ";
if (
$C12 == "ON" ) $sql .= "attachhd, ";
if (
$C13 == "ON" ) $sql .= "software, ";
if (
$C14 == "ON" ) $sql .= "weblog, ";
if (
$C15 == "ON" ) $sql .= "other ";



if (
substr($sql, -2) == ", " ) $sql = substr($sql, 0, strlen($str)-2) . " ";

$sql .= "from pcinfo order by hdid asc";

$link = mysql_connect('127.0.0.1','root','0000') or die("無法連線!請連絡管理者!");

mysql_select_db('hdinfo',$link) or die("無法連到資料庫!請連絡管理者!");

//echo $sql;

$result = mysql_query("$sql");

$total=mysql_num_rows($result);

//echo $total;

if ( $HTTP_POST_VARS["C1"] == "ON" ) echo "硬件编号\t";
if (
$C2 == "ON" ) echo "所属厂别\t";
if (
$C3 == "ON" ) echo "所分配的IP\t";
if (
$C4 == "ON" ) echo "机器型号\t";
if (
$C5 == "ON" ) echo "使用者名称\t";
if (
$C6 == "ON" ) echo "使用者部门\t";
if (
$C7 == "ON" ) echo "内存配置\t";
if (
$C8 == "ON" ) echo "CPU配置\t";
if (
$C9 == "ON" ) echo "硬盘配置\t";
if (
$C10 == "ON" ) echo "网卡型号及MAC\t";
if (
$C11 == "ON" ) echo "显示器型号\t";
if (
$C12 == "ON" ) echo "其所附的硬件\t";
if (
$C13 == "ON" ) echo "所安装的软件\t";
if (
$C14 == "ON" ) echo "上网申请纪录\t";
if (
$C15 == "ON" ) echo "其他\t";
echo
" \t\n";

for (
$i=0 ;$i<$total; $i++)
{
$result_row=mysql_fetch_row($result);
while (list(
$no,$content)=each($result_row))
{
echo
"$content\t";
}
echo
" \t\n";
}

以上是根据不同的选项查询不同的列,然后生成Excel 的文件