传递日期从php页面导出到excel文件 [英] Passing Date from php page to export to excel document

查看:95
本文介绍了传递日期从php页面导出到excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还有另一个问题!我原来是这里
在php和仅显示年份



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

  $ 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); //过去的日期
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;'); //这应该适用于IE& Opera
header(Content-type:application / x-msexcel); //这应该适用于其他
标头('Content-Disposition:attachment; filename ='。basename($ export_file)。'');

if(!($ dbconnect = mysql_pconnect(localhost,dbname,pass))){
print(无法连接到数据库!\\\
) ;
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(。*)#' $ 2- $ 1 $ 4',$ fromdate);

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

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


解决方案

好的,我设法弄清楚了一个朋友!



正在创建excel文档的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'];

就是这样。我是一个noob,所以我可能没有解释太好。希望这可能对某人有用!


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

解决方案

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!

这篇关于传递日期从php页面导出到excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆