<?php

require_once "../excel/PHPExcel.php";
require_once "../excel/PHPExcel/Writer/Excel2007.php";
require_once "../config/database.php";
require_once "../model/AdminDb.php";

$AdminDb=new AdminDb();
$time=time();
$time=date("Y-m-d");
$fileName=$time;
$dataAll=$AdminDb->queryBed();
$sun=count($dataAll);
    if(empty($filename)) $filename = time();
    
    $excel2007=false;

     $indexKey = array('id','build','unit','floor','room');
     $startRow = 1;
     $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z');

    //初始化PHPExcel()

    $objPHPExcel = new PHPExcel();

    //設定儲存版本格式

    if($excel2007){

        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

        $filename = $filename.'.xlsx';

    }else{

        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        $filename = $filename.'.xls';

    }
    
    //接下來就是寫資料到表格裡面去
    $objActSheet = $objPHPExcel->getActiveSheet();
    
    foreach ($dataAll as $row) {

        foreach ($indexKey as $key => $value){

            //這裡是設定單元格的內容

            $objActSheet->setCellValue($header_arr[$key].$startRow,$row[$value]);

        }
        $startRow++;
    }
    // 下載這個表格,在瀏覽器輸出
    header("Pragma: public");

    header("Expires: 0");

    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");

    header("Content-Type:application/force-download");

    header("Content-Type:application/vnd.ms-execl");

    header("Content-Type:application/octet-stream");

    header("Content-Type:application/download");;

    header('Content-Disposition:attachment;filename='.$filename.'');

    header("Content-Transfer-Encoding:binary");

    $objWriter->save('php://output');