Passing Date from php page to export to excel docu

2019-09-07 10:55发布

I am having yet another problem! My original is here Passing date between pages php and displaying year only

What I need to do now is enable that date to be used for the data which is exported to an excel doc. I have tried to add the normal parameters at the top but it just ignores them! This is the code I have at the top to create the excel doc. I hope someone can help.

$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));

标签: php excel date
1条回答
狗以群分
2楼-- · 2019-09-07 11:14

Ok I managed to figure it out with the help of a friend!

The php file that was creating the excel doc could not get the values as they were not being passed properly for some reason. The way around this was to add it to the session!

This was used on the page sending the date

<?
        $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;
        ?>

This was used on the excel php page

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

That was it. I am a noob so I may not have explained it too well. Hope it might be of some use for someone!

查看更多
登录 后发表回答