request->post(); // $path = ROOT_PATH . 'public' . DS . 'welfare' . DS.$data['dir']; // if (!is_dir($path)) { // return false; // } // $arr = array(); // $data = scandir($path); // foreach ($data as $value) { // if ($value != '.' && $value != '..') { // $arr[] = $value; // } // } // $re['files'] = $arr; // return jsonSuccess($re); // } public function getLastFile() { $data = $this->request->post(); $file = ROOT_PATH . 'public' . DS . "welfare" . DS . $data['filename']; $savefile = ROOT_PATH . 'public' . DS . "zch" . DS . substr($data['filename'],0,strrpos($data['filename'],'.')).'.docx'; $medir = "zch" . DS . substr($data['filename'],0,strrpos($data['filename'],'.')).'.docx'; if(is_file($savefile)){ $re['file'] = $medir; return jsonSuccess($re); } $res = $this->readExcel($file); $frag = []; $fraz = []; $frad = []; foreach ($res as $k => $v) { if ($v['level'] == '高风险') { if (!isset($frag[$v['p']][$v['city']]['area'])) { isset($frag[$v['p']]) ?: $frag[$v['p']] = []; isset($frag[$v['p']][$v['city']]) ?: $frag[$v['p']][$v['city']] = []; $frag[$v['p']][$v['city']]['area'] = []; $frag[$v['p']][$v['city']]['area'][] = $v['area']; } else { if (!in_array($v['area'], $frag[$v['p']][$v['city']]['area'])) { $frag[$v['p']][$v['city']]['area'][] = $v['area']; } } } if ($v['level'] == '中风险') { if (!isset($fraz[$v['p']][$v['city']]['area'])) { isset($fraz[$v['p']]) ?: $fraz[$v['p']] = []; isset($fraz[$v['p']][$v['city']]) ?: $fraz[$v['p']][$v['city']] = []; $fraz[$v['p']][$v['city']]['area'] = []; $fraz[$v['p']][$v['city']]['area'][] = $v['area']; } else { if (!in_array($v['area'], $fraz[$v['p']][$v['city']]['area'])) { $fraz[$v['p']][$v['city']]['area'][] = $v['area']; } } } if ($v['level'] == '低风险') { if (!isset($frad[$v['p']][$v['city']]['area'])) { isset($frad[$v['p']]) ?: $frad[$v['p']] = []; isset($frad[$v['p']][$v['city']]) ?: $frad[$v['p']][$v['city']] = []; $frad[$v['p']][$v['city']]['area'] = []; $frad[$v['p']][$v['city']]['area'][] = $v['area']; } else { if (!in_array($v['area'], $frad[$v['p']][$v['city']]['area'])) { $frad[$v['p']][$v['city']]['area'][] = $v['area']; } } } } $fg = []; foreach ($frag as $k => $v) { foreach ($v as $k1 => $v1) { $c['p'] = $k; $c['city'] = $k1; $c['area'] = implode(',', $v1['area']); $fg[] = $c; } } $fz = []; foreach ($fraz as $k => $v) { foreach ($v as $k1 => $v1) { $c['p'] = $k; $c['city'] = $k1; $c['area'] = implode(',', $v1['area']); $fz[] = $c; } } $fd = []; foreach ($frad as $k => $v) { foreach ($v as $k1 => $v1) { $c['p'] = $k; $c['city'] = $k1; $c['area'] = implode(',', $v1['area']); $fd[] = $c; } } // //生成excel // $obj_phpexcel = new PHPExcel(); //创建类实例 // $obj_phpexcel->setActiveSheetIndex(0); //设置当前页 // dump($fg); // dump($fz); // dump($fd); $PHPWord = new PhpWord(); $section = $PHPWord->addSection(); // $section = $PHPWord->createSection(); $PHPWord->addTitleStyle(1, array('bold' => true, 'size' => 18, 'name' => 'Arial', 'Color' => '333'), array('align' => 'center')); // $section->addTitle("疫情防控", 1); //定义样式数组 $styleTable = array( 'borderSize' => 6, 'borderColor' => '000000', 'cellMargin' => 150 ); $styleFirstRow = array( 'borderBottomSize' => 18, 'borderBottomColor' => '000000', 'bgColor' => '66bbff' ); // $cellStyle = array('gridSpan' => 2); $cellStyle1 = array('gridSpan' => 3, "bgColor" => "FF0000", 'align' => 'center'); $cellStyle2 = array('gridSpan' => 3, "bgColor" => "EEEE11", 'align' => 'center'); $cellStyle3 = array('gridSpan' => 3, "bgColor" => "0099FF", 'align' => 'center'); //添加表格样式 $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow); //添加表格 $table = $section->addTable('myOwnTableStyle'); $table->addRow(); $table->addCell(9000, $cellStyle1)->addText('高风险等级地区'); $gn = ""; foreach ($fg as $v) { $table->addRow(); if ($v['p'] == $gn) { $table->addCell(2000, array('vMerge' => 'continue')); } else { $table->addCell(2000, array('vMerge' => 'restart', 'valign' => 'center'))->addText($v['p']); } $table->addCell(2000)->addText($v["city"]); $table->addCell(5000)->addText($v['area']); $gn = $v['p']; } $table = $section->addTable('myOwnTableStyle'); $table->addRow(); $table->addCell(9000, $cellStyle2)->addText('中风险等级地区'); $zn = ""; foreach ($fz as $v) { $table->addRow(); if ($v['p'] == $zn) { $table->addCell(2000, array('vMerge' => 'continue')); } else { $table->addCell(2000, array('vMerge' => 'restart', 'valign' => 'center'))->addText($v['p']); } $table->addCell(2000)->addText($v["city"]); $table->addCell(5000)->addText($v['area']); $zn = $v['p']; } $table1 = $section->addTable('myOwnTableStyle'); $table1->addRow(); $table1->addCell(9000, $cellStyle3)->addText('低风险等级地区'); $dn = ""; foreach ($fd as $v) { $table1->addRow(); if ($v['p'] == $dn) { $table1->addCell(2000, array('vMerge' => 'continue')); } else { $table1->addCell(2000, array('vMerge' => 'restart', 'valign' => 'center'))->addText($v['p']); } $table1->addCell(2000)->addText($v["city"]); $table1->addCell(5000)->addText($v['area']); $dn = $v['p']; } // $table1 = $section->addTable('myOwnTableStyle'); // $table1->addRow(); // $table1->addCell(9000,$cellStyle2)->addText('中风险等级地区'); // $zn = ""; // foreach($fz as $v){ // $table1->addRow(); // if($v['p']==$zn){ // $table->addCell(2000,array('vMerge' => 'continue')); // }else{ // $table->addCell(2000, array('vMerge' => 'restart'))->addText($v['p']); // } // $table1->addCell(2000)->addText($v["city"]); // $table1->addCell(5000)->addText($v['area']); // $zn = $v['p']; // } // $table2 = $section->addTable('myOwnTableStyle'); // $table2->addRow(); // $table2->addCell(9000,$cellStyle3)->addText('低风险等级地区'); // $dn = ""; // foreach($fd as $v){ // $table2->addRow(); // if($v['p']==$dn){ // $table->addCell(2000,array('vMerge' => 'continue')); // }else{ // $table->addCell(2000, array('vMerge' => 'restart'))->addText($v['p']); // } // $table2->addCell(2000)->addText($v["city"]); // $table2->addCell(5000)->addText($v['area']); // $dn = $v['p']; // } // $table->addRow(); // $table->addCell(3000)->addText('测试1'); // $table->addCell(3000)->addText('测试2'); // $table->addCell(3000)->addText('测试3'); // $table->addRow(); // $table->addCell(3000)->addText('测试4'); // $table->addCell(6000, $cellStyle)->addText('测试5'); //合并列单元格 // $table->addRow(); // $table->addCell(3000, array('vMerge' => 'restart'))->addText('合并'); // $table->addCell(3000)->addText('测试6'); // $table->addCell(3000)->addText('测试7'); // $table->addRow(); // $table->addCell(3000, array('vMerge' => 'continue')); // $table->addCell(3000)->addText('测试8'); // $table->addCell(3000)->addText('测试9'); // $table->addRow(); // $table->addCell(3000,array('vMerge' => 'continue')); // $table->addCell(3000)->addText('测试10'); // $table->addCell(3000)->addText('测试11'); $file = '风险区域总结.docx'; //文档存在服务器上用此代码 $objWriter = IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save($savefile); $re['file'] = $medir; return jsonSuccess($re); //直接下载不存储在服务器上用此代码 // header("Content-Description: File Transfer"); // header('Content-Disposition: attachment; filename="' . $file . '"'); // header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); // header('Content-Transfer-Encoding: binary'); // header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); // header('Expires: 0'); // $xmlWriter = IOFactory::createWriter($PHPWord, 'Word2007'); // $xmlWriter->save("php://output"); } /** * 读取excel数据 */ private function readExcel($path) { $extension = substr($path, strrpos($path, '.') + 1); vendor("PHPExcel.PHPExcel"); if ($extension == 'xlsx') { $objReader = new \PHPExcel_Reader_Excel2007(); $objPHPExcel = $objReader->load($path); } else if ($extension == 'xls') { $objReader = new \PHPExcel_Reader_Excel5(); $objPHPExcel = $objReader->load($path); } $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); $frag = []; for ($i = 3; $i <= $highestRow; $i++) { // $aa['username'] = $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue(); // $aa['has'] = '未出证'; // if($aa['username']==''){ // continue; // } $aa['p'] = $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue(); $aa['city'] = $objPHPExcel->getActiveSheet()->getCell("C" . $i)->getValue(); $aa['area'] = $objPHPExcel->getActiveSheet()->getCell("D" . $i)->getValue(); $aa['address'] = $objPHPExcel->getActiveSheet()->getCell("E" . $i)->getValue(); $aa['level'] = $objPHPExcel->getActiveSheet()->getCell("F" . $i)->getValue(); $frag[] = $aa; } return $frag; } public function up_excel_file() { $file = request()->file('welfare'); if ($file) { $info = $file->move(ROOT_PATH . 'public' . DS . 'welfare', ''); if ($info) { return json(['code' => 0, 'msg' => 'success', 'upurl' => str_replace("\\", "/", $info->getSaveName())]); } else { return json(['code' => 1, 'msg' => $file->getError()]); } } } }