:::

7-5-4 產生完整的 Excel 匯出檔

您沒有觀看影片的權限

您沒有觀看影片的權限

請先登入,登入後,確認您的權限後,即可觀看影片。

  1. 編輯 excel.php
    <?php
    use Xmf\Request;
    use XoopsModules\Tad_signup\Tad_signup_actions;
    use XoopsModules\Tad_signup\Tad_signup_data;
    /*-----------引入檔案區--------------*/
    require_once __DIR__ . '/header.php';
    
    if (!$_SESSION['can_add']) {
        redirect_header($_SERVER['PHP_SELF'], 3, "您沒有權限使用此功能");
    }
    
    $id = Request::getInt('id');
    $type = Request::getString('type');
    
    $action = Tad_signup_actions::get($id);
    if ($action['uid'] != $xoopsUser->uid()) {
        redirect_header($_SERVER['PHP_SELF'], 3, "您沒有權限使用此功能");
    }
    
    require_once XOOPS_ROOT_PATH . '/modules/tadtools/vendor/phpoffice/phpexcel/Classes/PHPExcel.php'; //引入 PHPExcel 物件庫
    require_once XOOPS_ROOT_PATH . '/modules/tadtools/vendor/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php'; //引入PHPExcel_IOFactory 物件庫
    $objPHPExcel = new PHPExcel(); //實體化Excel
    //----------內容-----------//
    $title = "{$action['title']}報名名單";
    $objPHPExcel->setActiveSheetIndex(0); //設定預設顯示的工作表
    $objActSheet = $objPHPExcel->getActiveSheet(); //指定預設工作表為 $objActSheet
    $objActSheet->setTitle($title); //設定標題
    $objPHPExcel->createSheet(); //建立新的工作表,上面那三行再來一次,編號要改
    
    // 抓出標題資料
    $head_row = explode("\n", $action['setup']);
    $head = [];
    foreach ($head_row as $head_data) {
        $cols = explode(',', $head_data);
        if (strpos($cols[0], '#') === false) {
            $head[] = str_replace('*', '', trim($cols[0]));
        }
    }
    $head[] = '錄取';
    $head[] = '報名日期';
    $head[] = '身份';
    $row = 1;
    foreach ($head as $column => $value) {
        $objActSheet->setCellValueByColumnAndRow($column, $row, $value); //直欄從0開始,橫列從1開始
    }
    
    // 抓出內容部份
    
    if ($type == 'signup') {
        $signup = Tad_signup_data::get_all($action['id']);
        foreach ($signup as $signup_data) {
            $iteam = [];
            foreach ($signup_data['tdc'] as $user_data) {
                $iteam[] = implode('|', $user_data);
            }
    
            if ($signup_data['accept'] === '1') {
                $iteam[] = '錄取';
            } elseif ($signup_data['accept'] === '0') {
                $iteam[] = '未錄取';
            } else {
                $iteam[] = '尚未設定';
            }
            $iteam[] = $signup_data['signup_date'];
            $iteam[] = $signup_data['tag'];
    
            $row++;
            foreach ($iteam as $column => $value) {
                $objActSheet->setCellValueByColumnAndRow($column, $row, $value); //直欄從0開始,橫列從1開始
            }
        }
    }
    
    // $title = (_CHARSET === 'UTF-8') ? iconv('UTF-8', 'Big5', $title) : $title;
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header("Content-Disposition: attachment;filename={$title}.xlsx");
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    
    // 避免excel下載錯誤訊息
    for ($i = 0; $i < ob_get_level(); $i++) {
        ob_end_flush();
    }
    ob_implicit_flush(1);
    ob_clean();
    $objWriter->setPreCalculateFormulas(false);
    $objWriter->save('php://output');
    exit;
    

     

新版提醒:取得標題及類型部份
$head_row = explode("\n", $action['setup']);
$head = [];
foreach ($head_row as $head_data) {
    $cols = explode(',', $head_data);
    if (strpos($cols[0], '#') === false) {
        $head[] = str_replace('*', '', trim($cols[0]));
    }
}
可以改用下列方式更簡單喔:
$TadDataCenter = new TadDataCenter('tad_signup');
$head = $TadDataCenter->getAllColItems($action['setup']);

tadtools/class/TadDataCenter.php 必須是 2021/10/29 日以後的版本,可至此下載覆蓋

link to https://github.com/tadlearn/tad_signup/commit/4e6a2ef95c7bd3f7a271f4e90b31232b521bca65 \


:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1769%26tbsn%3D48

書籍目錄

展開 | 闔起

線上使用者

81人線上 (12人在瀏覽線上書籍)

會員: 0

訪客: 81

更多…