ecshop使用PHPEXCEL批量匯出訂單資訊
阿新 • • 發佈:2019-01-08
2.admin/templates/order_list.htm,在
<input name="print" type="submit" id="btnSubmit4" value="{$lang.print_order}" class="button" disabled="true" onclick="this.form.target = '_blank'" />
後,新增<input name="export" type="submit" id="btnSubmit5" value="匯出為EXCEL" class="button" disabled="true" onclick="this.form.target = '_blank'" />
3.admin/order.php,在‘/* 批量列印訂單 */’前,新增
效果如圖所示:/* 批量匯出訂單 */ elseif (isset($_POST['export'])) { if (empty($_POST['order_id'])) { sys_msg($_LANG['pls_select_order']); } /* 賦值公用資訊 */ $smarty->assign('shop_name', $_CFG['shop_name']); $smarty->assign('shop_url', $ecs->url()); $smarty->assign('shop_address', $_CFG['shop_address']); $smarty->assign('service_phone', $_CFG['service_phone']); $smarty->assign('print_time', local_date($_CFG['time_format'])); $smarty->assign('action_user', $_SESSION['admin_name']); $html = ''; $order_sn_list = explode(',', $_POST['order_id']); ////////////////////////// error_reporting(E_ALL); date_default_timezone_set('Europe/London'); require_once(ROOT_PATH . 'Classes/PHPExcel.php'); echo date('H:i:s') . " Create new PHPExcel object\n"; $objPHPExcel = new PHPExcel(); echo date('H:i:s') . " Set properties\n"; $objPHPExcel->getProperties()->setCreator("wdz")->setLastModifiedBy("wdz")->setTitle("我的訂單")->setSubject("我的訂單")->setDescription(date('Y/m/d H:i:s') . "匯出的訂單")->setKeywords("我的訂單")->setCategory("Test result file"); /*設定標題屬性*/ //字型大小 $objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getFont()->setSize(16); //加粗 $objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getFont()->setBold(true); //水平居中 $objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中 $objPHPExcel->getActiveSheet()->getStyle('A1:L1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //表格寬度 $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(40); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(8); $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(8); $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10); $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(13); $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(18); $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(18); echo date('H:i:s') . " Add some data\n"; $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->setCellValue('A1', '訂單號'); $objPHPExcel->getActiveSheet()->setCellValue('B1', '訂貨日期'); $objPHPExcel->getActiveSheet()->setCellValue('C1', '款號'); $objPHPExcel->getActiveSheet()->setCellValue('D1', '商品名稱'); $objPHPExcel->getActiveSheet()->setCellValue('E1', '屬性'); $objPHPExcel->getActiveSheet()->setCellValue('F1', '數量'); $objPHPExcel->getActiveSheet()->setCellValue('G1', '價格'); $objPHPExcel->getActiveSheet()->setCellValue('H1', '收件人'); $objPHPExcel->getActiveSheet()->setCellValue('I1', '地址'); $objPHPExcel->getActiveSheet()->setCellValue('J1', '電話'); $objPHPExcel->getActiveSheet()->setCellValue('K1', '郵箱'); $objPHPExcel->getActiveSheet()->setCellValue('L1', '發貨日期'); $hang = 2; /////////////////////// foreach ($order_sn_list as $order_sn) { /* 取得訂單資訊 */ $order = order_info(0, $order_sn); if (empty($order)) { continue; } /* 根據訂單是否完成檢查許可權 */ if (order_finished($order)) { if (!admin_priv('order_view_finished', '', false)) { continue; } } else { if (!admin_priv('order_view', '', false)) { continue; } } /* 如果管理員屬於某個辦事處,檢查該訂單是否也屬於這個辦事處 */ $sql = "SELECT agency_id FROM " . $ecs->table('admin_user') . " WHERE user_id = '$_SESSION[admin_id]'"; $agency_id = $db->getOne($sql); if ($agency_id > 0) { if ($order['agency_id'] != $agency_id) { continue; } } /* 取得使用者名稱 */ if ($order['user_id'] > 0) { $user = user_info($order['user_id']); if (!empty($user)) { $order['user_name'] = $user['user_name']; } } /* 取得區域名 */ $sql = "SELECT concat(IFNULL(c.region_name, ''), ' ', IFNULL(p.region_name, ''), " . "' ', IFNULL(t.region_name, ''), ' ', IFNULL(d.region_name, '')) AS region " . "FROM " . $ecs->table('order_info') . " AS o " . "LEFT JOIN " . $ecs->table('region') . " AS c ON o.country = c.region_id " . "LEFT JOIN " . $ecs->table('region') . " AS p ON o.province = p.region_id " . "LEFT JOIN " . $ecs->table('region') . " AS t ON o.city = t.region_id " . "LEFT JOIN " . $ecs->table('region') . " AS d ON o.district = d.region_id " . "WHERE o.order_id = '$order[order_id]'"; $order['region'] = $db->getOne($sql); /* 其他處理 */ $order['order_time'] = local_date($_CFG['time_format'], $order['add_time']); $order['pay_time'] = $order['pay_time'] > 0 ? local_date($_CFG['time_format'], $order['pay_time']) : $_LANG['ps'][PS_UNPAYED]; $order['shipping_time'] = $order['shipping_time'] > 0 ? local_date($_CFG['time_format'], $order['shipping_time']) : $_LANG['ss'][SS_UNSHIPPED]; $order['status'] = $_LANG['os'][$order['order_status']] . ',' . $_LANG['ps'][$order['pay_status']] . ',' . $_LANG['ss'][$order['shipping_status']]; $order['invoice_no'] = $order['shipping_status'] == SS_UNSHIPPED || $order['shipping_status'] == SS_PREPARING ? $_LANG['ss'][SS_UNSHIPPED] : $order['invoice_no']; /* 此訂單的發貨備註(此訂單的最後一條操作記錄) */ $sql = "SELECT action_note FROM " . $ecs->table('order_action') . " WHERE order_id = '$order[order_id]' AND shipping_status = 1 ORDER BY log_time DESC"; $order['invoice_note'] = $db->getOne($sql); /* 引數賦值:訂單 */ $smarty->assign('order', $order); $shuliang = 0; /* 取得訂單商品 */ $goods_list = array(); $goods_attr = array(); $sql = "SELECT o.*,oi.order_sn, g.goods_number AS storage, o.goods_attr, IFNULL(b.brand_name, '') AS brand_name " . "FROM " . $ecs->table('order_goods') . " AS o " . "LEFT JOIN " . $ecs->table('goods') . " AS g ON o.goods_id = g.goods_id " . "LEFT JOIN " . $ecs->table('brand') . " AS b ON g.brand_id = b.brand_id LEFT JOIN" . $ecs->table('order_info'). " AS oi ON oi.order_id = o.order_id " . "WHERE o.order_id = '$order[order_id]' "; $res = $db->query($sql); $shuliang = 0; $chanpin = $hang; while ($row = $db->fetchRow($res)) { $shuliang = $shuliang + 1; /* 虛擬商品支援 */ if ($row['is_real'] == 0) { /* 取得語言項 */ $filename = ROOT_PATH . 'plugins/' . $row['extension_code'] . '/languages/common_' . $_CFG['lang'] . '.php'; if (file_exists($filename)) { include_once($filename); if (!empty($_LANG[$row['extension_code'] . '_link'])) { $row['goods_name'] = $row['goods_name'] . sprintf($_LANG[$row['extension_code'] . '_link'], $row['goods_id'], $order['order_sn']); } } } $objPHPExcel->getActiveSheet()->setCellValue('C' . $chanpin, $row['goods_sn']); $objPHPExcel->getActiveSheet()->setCellValue('D' . $chanpin, $row['goods_name']); $objPHPExcel->getActiveSheet()->setCellValue('E' . $chanpin, $row['goods_attr']); $objPHPExcel->getActiveSheet()->setCellValue('F' . $chanpin, $row['goods_number']); $objPHPExcel->getActiveSheet()->setCellValue('G' . $chanpin, $row['goods_price']); $row['formated_subtotal'] = price_format($row['goods_price'] * $row['goods_number']); $row['formated_goods_price'] = price_format($row['goods_price']); $goods_attr[] = explode(' ', trim($row['goods_attr'])); //將商品屬性拆分為一個數組 $goods_list[] = $row; $chanpin = $chanpin + 1; } $attr = array(); $arr = array(); foreach ($goods_attr AS $index => $array_val) { foreach ($array_val AS $value) { $arr = explode(':', $value); //以 : 號將屬性拆開 $attr[$index][] = @array( 'name' => $arr[0], 'value' => $arr[1] ); } } $smarty->assign('goods_attr', $attr); $smarty->assign('goods_list', $goods_list); $smarty->template_dir = '../' . DATA_DIR; $objPHPExcel->getActiveSheet()->setCellValue('A' . ($hang), $order['order_sn']); $objPHPExcel->getActiveSheet()->setCellValue('B' . ($hang), $order['order_time']); $objPHPExcel->getActiveSheet()->setCellValue('H' . ($hang), $order['consignee']); $objPHPExcel->getActiveSheet()->setCellValue('I' . ($hang), $order['address']); $objPHPExcel->getActiveSheet()->setCellValue('J' . ($hang), $order['tel']); $objPHPExcel->getActiveSheet()->setCellValue('K' . ($hang), $order['email']); $objPHPExcel->getActiveSheet()->setCellValue('L' . ($hang), $order['shipping_time']); for ($kk = $hang; $kk < ($hang + $shuliang); $kk++) { $objPHPExcel->getActiveSheet()->mergeCells('A' . $hang . ':A' . $kk); $objPHPExcel->getActiveSheet()->getStyle('A' . $hang . ':A' . $kk)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER); $objPHPExcel->getActiveSheet()->mergeCells('B' . $hang . ':B' . $kk); $objPHPExcel->getActiveSheet()->mergeCells('H' . $hang . ':H' . $kk); $objPHPExcel->getActiveSheet()->mergeCells('I' . $hang . ':I' . $kk); $objPHPExcel->getActiveSheet()->mergeCells('J' . $hang . ':J' . $kk); $objPHPExcel->getActiveSheet()->mergeCells('K' . $hang . ':K' . $kk); $objPHPExcel->getActiveSheet()->mergeCells('G' . $hang . ':G' . $kk); $objPHPExcel->getActiveSheet()->mergeCells('L' . $hang . ':L' . $kk); } $hang = $hang + $shuliang; } $objPHPExcel->getActiveSheet()->setTitle(("我的訂單")); $objPHPExcel->setActiveSheetIndex(0); require_once(ROOT_PATH . 'Classes/PHPExcel/IOFactory.php'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save(str_replace('.php', '.xls', __FILE__)); $url = "order.xls"; ecs_header("Location: $url\n"); exit; }