yijia-webman/app/controller/JglController.php

73 lines
1.9 KiB
PHP

<?php
namespace app\controller;
use support\Request;
use PhpOffice\PhpSpreadsheet\IOFactory;
//use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class JglController
{
public function index(Request $request)
{
$publicPath = public_path();
ini_set('memory_limit', '1G');
$file = $publicPath . '/data/data.xls';
$savePath = $publicPath . '/data/data2.xls';
$spreadsheet = IOFActory::load($file);
// 获取第一个工作表
$worksheet = $spreadsheet->getActiveSheet();
// 获取最大行数和列数
$maxRow = $worksheet->getHighestRow();
//$maxColumn = $worksheet->getHighestColumn();
//$saveSpreadsheet = new Spreadsheet();
//$saveActiveWorksheet = $saveSpreadsheet->getActiveSheet();
// 遍历每一行和每一列
//$stuList = [];
for ($row = 4; $row <= $maxRow; $row++)
{
$project = $worksheet->getCell('F' . $row)->getValue();
if(substr($project, 0, 1) == '='){
$project = $worksheet->getCell('E' . $row)->getValue();
}
$subProject = $this->getProject($project);
$worksheet->setCellValue('G' . $row, $subProject);
}
//$activeWorksheet = $spreadsheet->getActiveSheet();
//$activeWorksheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save($savePath);
exit;
}
private function getProject($project)
{
$projects = ['田径','篮球','足球','排球','武术','体操'];
$k = mt_rand(0, 5);
$tmpProject = $projects[$k];
while($tmpProject == $project){
$k = mt_rand(0, 5);
$tmpProject = $projects[$k];
echo "$tmpProject ==== $project\r\n";
}
echo "--------------------------------\r\n";
return $tmpProject;
}
}