doudui1850 2014-03-26 21:41
浏览 43

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

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!
");
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条回答 默认 最新

  • doudou8783 2014-03-27 20:50
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?