读取文件
1 $spreadsheet = IOFactory::createReader('Csv')//传文件类型首字母大写
->setReadDataOnly(true)//设置为只读取数据
->setReadEmptyCells(false)//不读取空白单元格
->load($path);//传文件路径
获取数据
$workSheet = $spreadsheet->getActiveSheet();//获取工作表($spreadsheet为加载文件后的变量) $totalRow = $workSheet->getHighestRow();//获取总行数 //通过循环读取单元格数据 $name = $workSheet->getCellByColumnAndRow($i, $j)->getValue();//该方法传列号和行号 //rangeToArray()将范围内的单元格转化为数组 $rangeArr = $workSheet->rangeToArray('A1:D3');
生成表格
//通过数组渲染表格数据,参数详见文档,可传二位数组和一维数组,这要看包的版本 $failedSpreadsheet->fromArray($failedDataHeader, NULL, 'A1');
设置样式
//设置某个单元格的颜色,也可以通过此方法设置更多不同的样式,但是若通过循环来设置每一个单元格的样式效率不高,推荐使用范围设置 $failedSpreadsheet->getStyle('A2:B3')->getFont()->getColor()->setARGB(Color::COLOR_RED); //范围设置 $styleArray = [ 'borders' => [//边框 'allBorders' => [ 'borderStyle' => Border::BORDER_THIN //细边框 ] ], 'alignment' => [//对齐 'horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER ] ]; $failedSpreadsheet->getStyle('A1:E6')->applyFromArray($styleArray);
保存表格
$failedfile = new PhpOffice\PhpSpreadsheet\Spreadsheet(); $writer = new PhpOffice\PhpSpreadsheet\Writer\Xlsx($failedfile); $writer->save($save_path);标签:总结,workSheet,单元格,spreadsheet,PhpSpreadsheet,failedSpreadsheet,设置,使用 From: https://www.cnblogs.com/Ronnie-97/p/16984333.html