ecshop网店系统默认没有导出订单的功能,有时候需要将订单批量导出,以便在第三方打印软件中打印,那么就需要一个支持将订单批量导出为excel的ecshop订单导出插件。万维景盛工程师为您分享使用PHPExcel第三方共享类来设计ecshop的订单批量导出插件的方法。
1、到github下载phpexcel类程序包,放到plugin目录下。phpexcel公共类免费下载地址
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; }