getActiveSheet(); // 表头单元格内容 第一行 $titCol = 'A'; foreach ($title as $value) { // 单元格内容写入 $sheet->setCellValue($titCol . '1', $value); $titCol++; } //单元格内容居中 $sheet->getDefaultColumnDimension()->setAutoSize(true);//列宽自适应 $sheet->getStyle('A:' . $titCol)->getAlignment()->setWrapText(true);//内容容垂居中 $sheet->getStyle('A:' . $titCol)->getAlignment()->setVertical('center');//内容容垂居中 $sheet->getStyle('A:' . $titCol)->getAlignment()->setHorizontal('center');//内水平直居中 // 从第二行开始写入数据 $row = 2; foreach ($data as &$item) { foreach ($item as &$value) { $dataCol = 'A'; foreach($value as &$v) { $sheet->setCellValue($dataCol . $row, $v); $len = strlen($v); if ($len > 1 ){ //空数据不做处理避免影响到有数据的列宽度 $sheet->getColumnDimension($dataCol)->setAutoSize(true); } $dataCol++; } $row++; } } $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); if (is_file($path . $file_name)) { file_put_contents($path . $file_name,'');; } $writer->save($path . $file_name); return array('url' => trim($file_name,'.')); }catch (\Exception $e){ } } }