A、1=>B、26=>AA...以此類推) function num2alpha($n) { for ($r = ""; $n >= 0; $n = intval($n / 26) - 1) $r = chr($n % 26 + 0x41) . $r; return $r; } //英文轉數字(A=>0、B=>1、AA=>26...以此類推) function alpha2num($a) { $l = strlen($a); $n = 0; for ($i = 0; $i < $l; $i++) $n = $n * 26 + ord($a[$i]) - 0x40; return $n - 1; } function getArea($address) { $address = mb_substr($address, 0, 2); if (in_array($address, ['宜蘭', '花蓮', '台東'])) return '宜花東'; if (in_array($address, ['臺北', '台北', '基隆', '新北'])) return '北區'; if (in_array($address, ['桃園', '新竹'])) return '桃竹'; if (in_array($address, ['南投', '臺中', '台中', '彰化', '雲林', '苗栗'])) return '中區'; if (in_array($address, ['台南', '臺南', '嘉義', '屏東', '高雄'])) return '南區'; } if ($_SERVER["REQUEST_METHOD"] == "POST") { $contractno = empty($_POST['contractno']) ? null : $_POST['contractno']; $facilityno = empty($_POST['facilityno']) ? null : $_POST['facilityno']; $custom = empty($_POST['custom']) ? null : $_POST['custom']; $site_survey_contact_verify = !isset($_POST['site_survey_contact_verify']) ? '' : $_POST['site_survey_contact_verify']; $real_contract_arrival_date_start = empty($_POST['real_contract_arrival_date_start']) ? null : $_POST['real_contract_arrival_date_start']; $real_contract_arrival_date_end = empty($_POST['real_contract_arrival_date_end']) ? null : $_POST['real_contract_arrival_date_end']; $area_no = !isset($_POST['area_no']) ? '' : $_POST['area_no']; // 取得當前使用者所屬部門 $department_id = getDepartmentId($link, $user_id); // 取得當前使用者所屬職位 $role_id = getRoleId($link, $user_id); $sql = getDataSql($department_id, $user_id, "A"); $result = mysqli_query($link, $sql); if ($result) { $data = array(); while ($row = $result->fetch_assoc()) { $data[] = $row; } $result->close(); } else { echo "Query failed: " . $connection->error; } // 建立一個新的 Spreadsheet 物件 $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $colTitleArr = [ '區域', '合約號', '作番號', '汰改方案', '客戶名稱', '合約地址', '工地地址', '規格', '依合約客戶需求日', '實際到廠日(觀音廠)', '預計出貨日(到工地)', '實際出貨日(到工地)', '工勘確認', '工勘確認日', '安裝廠商', '實際發包日', '預計開工日', '實際開工日', '預計完工日', '實際完工日', '試車廠商', '實際發包日', '預計開工日', '實際開工日', '預計完工日', '實際完工日', '內部QC日', 'QC合格日', '官檢日', '移交日', '營業人員' ]; $sheet->setTitle('汰改出貨預定表'); for ($i = 0; $i < count($colTitleArr); $i++) $sheet->setCellValue(num2alpha($i) . '1', $colTitleArr[$i]); $i = 2; foreach ($data as $row) { $two_row_status = false; if (!empty($row['install_outsourcer']) && !empty($row['tryrun_outsourcer'])) $two_row_status = true; if (!empty($row['install_outsourcer_date']) && !empty($row['tryrun_outsourcer_date'])) $two_row_status = true; if (!empty($row['install_start_date']) && !empty($row['tryrun_start_date'])) $two_row_status = true; if (!empty($row['estimate_install_end_date']) && !empty($row['estimate_tryrun_end_date'])) $two_row_status = true; //規格 $Specification = $row['facility_kind']; $Specification .= !empty($row['numberofpassenger']) ? "-" . $row['numberofpassenger'] : ""; $Specification .= !empty($row['weight']) ? "-" . $row['weight'] : ""; $Specification .= !empty($row['numberofstop']) ? "-" . $row['numberofstop'] : ""; $Specification .= !empty($row['numberoffloor']) ? "/" . $row['numberoffloor'] : ""; $Specification .= !empty($row['opentype']) ? "-" . $row['opentype'] : ""; $Specification .= !empty($row['speed']) ? $row['speed'] : ""; // 工務統計已確認 $gongwuok = 0; if ($row['building_heigh_verify'] == 0) $gongwuok += 1; // if ($row['site_survey_contact_verify'] == 0) if (!in_array($row['site_survey_contact_verify'], ['1', 'A'])) $gongwuok += 1; $colContentArr = [ getArea($row['address']), $row['contractno'], $row['facilityno'], implode("+", json_decode($row['renovate_type'])), $row['custom'], $row['address'], $row['real_address'], $Specification, substr($row['contract_arrival_date'], 0, 10), substr($row['actual_tofactory_date'], 0, 10), substr($row['real_contract_arrival_date'], 0, 10), substr($row['real_arrival_date'], 0, 10), getGunwuName($row['address']) . $gongwuok . "/2", substr($row['site_survey_contact_verify_at'], 0, 10), $row['install_outsourcer'], substr($row['install_outsourcer_date'], 0, 10), substr($row['estimate_install_start_date'], 0, 10), substr($row['install_start_date'], 0, 10), substr($row['estimate_install_end_date'], 0, 10), substr($row['install_end_date'], 0, 10), $row['tryrun_outsourcer'], substr($row['tryrun_outsourcer_date'], 0, 10), substr($row['estimate_tryrun_start_date'], 0, 10), substr($row['tryrun_start_date'], 0, 10), substr($row['estimate_tryrun_end_date'], 0, 10), substr($row['tryrun_end_date'], 0, 10), substr($row['qc_date'], 0, 10), substr($row['end_qc_date'], 0, 10), substr($row['official_check_date'], 0, 10), substr($row['delivery_date'], 0, 10), accountidToName($row['salesid']) ]; for ($j = 0; $j < count($colContentArr); $j++) { $sheet->setCellValue(num2alpha($j) . $i, $colContentArr[$j]); } $i++; } $writer = new Xlsx($spreadsheet); $file_path = dirname(__DIR__) . '/wms/excel/' . 'wipwholeintall.xlsx'; try { $writer->save($file_path); // 回傳檔案路徑給 JavaScript echo $file_path; } catch (Exception $e) { echo 'Error: ' . $e->getMessage(); } exit(); }