一.业务需求
1.新增了销售合同金额的字段,但是老数据没有这个字段;所以销售合同金额从销售合同附件的各品种金额之和。
2.制作好excel字段模板,将此模板发送给销售业务部门来统计并完成excel表格数据
二.产品设计
无
三.UI设计
无
四.程序设计
1.使用php-excel插件封装ExcelHelper类,包括导出数据到excel和将excel数据导入数据库
<?php namespace core\components; use PHPExcel; use PHPExcel_IOFactory; use PHPExcel_Style_Alignment; use PHPExcel_Reader_Excel5; use PHPExcel_RichText; class MyExcelHelper extends \yii\base\Component{ /** * 将二维数组的数据转化为excel表格导出 * @param $data * @param $excel_name * @param $headers * @param $options */ public static function array2excel($data, $excel_name, $headers, $options, $style_options){ $objPHPExcel = new PHPExcel(); ob_start(); if (!isset($options['creator'])){ $objPHPExcel->getProperties()->setCreator('creator'); }else{ $objPHPExcel->getProperties()->setCreator($options['creator']); } if (isset($options['last_modified_by'])){ $objPHPExcel->getProperties()->setCreator('last_modified_by'); }else{ $objPHPExcel->getProperties()->setCreator($options['last_modified_by']); } if (isset($options['title'])){ $objPHPExcel->getProperties()->setCreator('title'); }else{ $objPHPExcel->getProperties()->setCreator($options['title']); } if (isset($options['subject'])){ $objPHPExcel->getProperties()->setCreator('subject'); }else{ $objPHPExcel->getProperties()->setCreator($options['subject']); } if (isset($options['description'])){ $objPHPExcel->getProperties()->setCreator('description'); }else{ $objPHPExcel->getProperties()->setCreator($options['description']); } if (isset($options['keywords'])){ $objPHPExcel->getProperties()->setCreator('keywords'); }else{ $objPHPExcel->getProperties()->setCreator($options['keywords']); } if (isset($options['category'])){ $objPHPExcel->getProperties()->setCreator('category'); }else{ $objPHPExcel->getProperties()->setCreator($options['category']); } $header_keys = array_keys($headers); foreach ($header_keys as $header_index => $header_key){ $index_ascii = $header_index + 65; $index_chr = chr($index_ascii); $header_value = $headers[$header_key]; $objPHPExcel->setActiveSheetIndex(0)->setCellValue($index_chr.'1', $header_value); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension($index_chr)->setWidth(20); $objPHPExcel->setActiveSheetIndex(0)->getStyle($index_chr.'1')->applyFromArray([ 'font'=>[ 'bold' => true ], 'alignment'=>[ 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER ] ]); if (isset($style_options['h_align'][$header_key])){ if ($style_options['h_align'][$header_key] == 'left'){ $h_align = PHPExcel_Style_Alignment::HORIZONTAL_LEFT; }elseif ($style_options['h_align'][$header_key] == 'center'){ $h_align = PHPExcel_Style_Alignment::HORIZONTAL_CENTER; }elseif ($style_options['h_align'][$header_key] == 'right'){ $h_align = PHPExcel_Style_Alignment::HORIZONTAL_RIGHT; }else{ $h_align = PHPExcel_Style_Alignment::HORIZONTAL_CENTER; } $objPHPExcel->setActiveSheetIndex(0)->getStyle($index_chr)->applyFromArray([ 'alignment'=>[ 'horizontal' => $h_align ] ]); } } $data_row_index = 2; foreach ($data as $row_index => $row){ $data_keys = array_keys($row); foreach ($data_keys as $column_index => $data_key){ if ($column_index>=26){ throw new \yii\base\Exception('EXCEL表格超过26列'); } $index_ascii = $column_index + 65; $index_chr = chr($index_ascii); $value = $row[$data_key]; $objPHPExcel->setActiveSheetIndex(0)->setCellValue($index_chr . $data_row_index, $value); } $data_row_index++; } if (isset($options['summary'])){ $summary_keys = array_keys($options['summary']); foreach ($summary_keys as $summary_index => $summary_key){ $index_ascii = $summary_index + 65; $index_chr = chr($index_ascii); $summary_flag = $options['summary'][$summary_key]; if ($summary_flag){ $summary_value = \core\components\ArrayHelper::sumByColumn($data, $summary_key); $objPHPExcel->setActiveSheetIndex(0)->setCellValue($index_chr . $data_row_index, $summary_value); } } } $objPHPExcel->setActiveSheetIndex(0); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); ob_end_clean(); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $excel_name . '.xls"'); header('Cache-Control: max-age=0'); $objWriter->save('php://output'); exit; } /** * 将excel表格转化为二维数组的数据 * @param $excel_path * @param $data * @param $header */ public static function excel2array($excel_path, $header_keys){ if(!file_exists($excel_path)){ throw new \yii\base\Exception('该EXCEL不存在!'); } $PHPReader = new \PHPExcel_Reader_Excel2007(); if(!$PHPReader->canRead($excel_path)){ $PHPReader = new PHPExcel_Reader_Excel5(); if(!$PHPReader->canRead($excel_path)){ throw new \yii\base\Exception('该EXCEL不可读'); } } $PHPExcel = $PHPReader->load($excel_path); $currentSheet = $PHPExcel->getSheet(0); $max_column_index = $currentSheet->getHighestColumn(); $max_row_index = $currentSheet->getHighestRow(); $data = array(); for($row_index=2; $row_index<=$max_row_index; $row_index++ ){ for($column_chr='A'; $column_chr<=$max_column_index; $column_chr++){ $column_ord = ord($column_chr); $column_index = $column_ord - 65; $key = $column_chr.$row_index; $value = $currentSheet->getCell($key)->getValue(); if($value instanceof PHPExcel_RichText){ $value = $value->__toString(); } $data[$row_index-1][$header_keys[$column_index]] = $value; } } return $data; } }
2.使用命令模式编写控制台程序将excel的数据导入到数据库
<?php namespace console\controllers; use yii\console\Controller; use Yii; class SalesContractController extends Controller { public function actionUploadExcel(){ $excel_path = './console/excels/sales_contract.xlsx'; $header_keys = ['sales_contract_code', 'sales_customers_name', 'sales_contract_sum_weight']; $data = \core\components\MyExcelHelper::excel2array($excel_path, $header_keys); if (!empty($data)){ foreach ($data as $sales_contract){ $salesContractModel = \core\models\SalesContract::find()->where([ 'sales_contract_code'=>$sales_contract['sales_contract_code'], ])->one(); if (!empty($salesContractModel)){ $salesContractModel->sales_contract_sum_weight = \common\models\Base::weightBcmul($sales_contract['sales_contract_sum_weight']); if ($salesContractModel->save()){ echo PHP_EOL.'导入销售合同'.$sales_contract['sales_contract_code'].'更新数据成功!'; } } } } } }
五.上线
上传excel到制定目录:
运行命令如下:
php yii sales-contract/upload-excel
运行结果如下: