:::

7-5-3 產生 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[] = '身份';
    
    foreach ($head as $column => $value) {
        $objActSheet->setCellValueByColumnAndRow($column, 1, $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/d3dd677879b5b8e09a4f57e1dbd7d19a42ecf534 \


:::

搜尋

QR Code 區塊

https%3A%2F%2Fmail.tad0616.cp27.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1754%26tbsn%3D48

書籍目錄

展開 | 闔起

線上使用者

362人線上 (146人在瀏覽線上書籍)

會員: 0

訪客: 362

更多…