从php页面传递日期以导出到excel文档

时间:2014-03-26 21:41:05

标签: php excel date

我还有另一个问题!我原来在这里 Passing date between pages php and displaying year only

我现在需要做的是将该日期用于导出到Excel文档的数据。我试图在顶部添加正常参数,但它只是忽略它们!这是我在顶部创建excel doc的代码。我希望有人可以提供帮助。

$export_file = "ExcelFile.xls";
ob_end_clean();
ini_set('zlib.output_compression','Off');

header('Pragma: public');
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");                  // Date in the past   
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");
header('Content-Transfer-Encoding: none');
header('Content-Type: application/vnd.ms-excel;');                 // This should work for IE & Opera
header("Content-type: application/x-msexcel");                    // This should work for the rest
header('Content-Disposition: attachment; filename="'.basename($export_file).'"'); 

if(!($dbconnect = mysql_pconnect("localhost", "dbname", "pass"))){
print("Failed to connect to database!\n");
exit();
}
$fromdatePost = $_POST["report_date_from"];
        $todatePost = $_POST["report_date_to"];

        $fromdate = $_POST["report_date_from"] .' 00:00:00';
        $fromdate = preg_replace('#(\d{2})/(\d{2})/(\d{4})\s(.*)#', '$3-$2-$1 $4', $fromdate);

        $todate = $_POST["report_date_to"] .' 00:00:00';
        $todate = preg_replace('#(\d{2})/(\d{2})/(\d{4})\s(.*)#', '$3-$2-$1 $4', $todate);

        $fromyear = date('Y', strtotime($fromdate));
        $toyear = date('Y', strtotime($todate));

1 个答案:

答案 0 :(得分:0)

好的,我设法在朋友的帮助下搞清楚了!

创建excel doc的php文件无法获取值,因为它们由于某种原因未正确传递。解决这个问题的方法是将它添加到会话中!

这是在发送日期的页面上使用的

<?
        $from = mysql_real_escape_string($_POST['report_date_from']);
        $_SESSION['report_date_from'] = $from;
        $to = mysql_real_escape_string($_POST['report_date_to']);
        $_SESSION['report_date_to'] = $to;
        ?>

这是在excel php页面上使用的

$fromdatePost = $_SESSION['report_date_from'];
    $todatePost = $_SESSION['report_date_to'];

就是这样。我是一个菜鸟,所以我可能没有解释得太清楚。希望它可能对某些人有用!