成功生成excel文件后如何创建xls文件

时间:2011-07-20 13:06:33

标签: php sql-server excel

我有这样的代码:

<?php
include "ExcelUtil.php";
include "admin/doconnect.php";

class ExcelGenerator {
    var $xls;

    function ExcelGenerator() 
    {
        $this->xls = new ExcelUtil();
    }
function saveToExcel($pageFunction, $searchdate1, $searchdate2, $person, $type, $ewonumber, $device1, $device2, $subjectx) 

    {        
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: application/vnd.ms-excel");
    header("Content-type: application/x-msexcel");
        header ("Content-Description: PHP/INTERBASE Generated Data");
        header("Content-Disposition: attachment;filename=Reportxls");
        header("Content-Transfer-Encoding: binary ");

$query = "select * from activity WHERE actiondate BETWEEN '$tanggal' AND '$now' order by actiondate DESC";
    $hasil = mssql_query($query);

    echo "<table width=\"97%\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\">";
    echo "<tr>";
    echo "<th>Action Date</th>";
    echo "<th>Person</th>";
    echo "<th>Subject</th>";
    echo "<th>EWO Number / TR Number</th>";
    echo "<th>Category</th>";
    echo "<th>Device</th>";
    echo "<th>Status</th>";
    echo "</tr>";
    while ($data = mssql_fetch_array($hasil))
    {
        echo "<tr valign='top'>";
        echo "<td class=\"isitabel\" align='center'>$data[actiondate]</td>";
        echo "<td class=\"isitabel\" align='left'>$data[person]</td>";
        echo "<td class=\"isitabel\" align='left'>$data[subject]</td>";
            if ($data[tr_number]=="" || $data[tr_number]=="-") {
            $data[tr_number] = "";
            }else if ($data[ewo_number]=="" || $data[ewo_number]=="-") {
                $data[ewo_number] = "";
                    }
        echo "<td class=\"isitabel\"align='center'>$data[ewo_number]$data[tr_number]</td>";
        echo "<td class=\"isitabel\" align='center'>$data[category]</td>";
        echo "<td class=\"isitabel\" align='center'>$data[device]</td>";
        echo "<td class=\"isitabel\" align='center'>$data[status]</td>";
        echo "</tr>";
        }
    echo "</table>";
    }
}

$generator = new ExcelGenerator();
$generator->saveToExcel();
?>

从该代码中,当出现下载提示时,我已成功转换为名为= Report.xls的xls文件。

在我的情况下,我想给一些代码将其xls文件自动写入我的目录(C:/)而无需用户必须在下载提示上单击“保存”。

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:3)

不,你当然不能这样做。

你能想象这个想法中的安全漏洞吗?

答案 1 :(得分:0)

不可能这样做。用户必须确认文件下载。