我需要生成一个大约有 1000 列的 Excel 文件。但在生成时,Laravel-Excel 库只能生成 256 列。
我几乎尝试了所有方法:我增加了memory_limit,max_execution_time,...但到目前为止没有任何效果。
版本
这是我尝试生成 1000 列的测试代码。
PHP
Excel::create("Test_A", function($excel) {
$excel->setTitle("Only test");
$excel->sheet("Test 1", function($sheet) {
$sheet->loadView('excel');
})->export('xls');
});
查看
<!DOCTYPE html>
<html lang="{{ App::getLocale() }}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test 1</title>
</head>
<body>
<table>
<thead>
<tr>
@for ($i = 0; $i < 1000; $i++)
<th>{{ $i }}</th>
@endfor
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
使用此代码使用 Laravel Excel 库导出 Excel。 您需要将数组列表作为第一个参数传递,如果您想添加带有徽标的标题,则第二个和第三个参数是可选的,然后使用它,否则修改一点并删除该标题部分。
function exportExcel($userList, $clientLogo, $defaultLogo) {
// Generate and return the spreadsheet
Excel::create('User_List', function ($excel) use ($userList,$clientLogo, $defaultLogo) {
// Set the spreadsheet title, creator, and description
$excel->setTitle('User List');
$excel->setCreator('CompanyName')->setCompany('CompanyName');
$excel->setDescription('User List Reports');
$excel->sheet('User List', function($sheet) use ($userList,$path,$clientLogo,$defaultLogo) {
//Set header with logo with different column width & height
$objDrawing = new PHPExcel_Worksheet_Drawing;
$objDrawing->setPath(public_path('img/'.$defaultLogo));
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($sheet);
$objDrawing->setName('Client Logo');
$objDrawing->setResizeProportional(false);
$objDrawing->setWidth(100);
$objDrawing->setHeight(50);
$sheet->setSize('A1', 20, 50);
$objDrawing = new PHPExcel_Worksheet_Drawing;
$objDrawing->setPath(public_path('img/'.$clientLogo));
$objDrawing->setCoordinates('D1');
$objDrawing->setWorksheet($sheet);
$objDrawing->setName('Client Logo');
$objDrawing->setResizeProportional(false);
$objDrawing->setWidth(100);
$objDrawing->setHeight(50);
$sheet->setSize('D1', 20, 50);
$sheet->fromArray($userList, null, 'A2', false, false);
$sheet->cells('A1:D1', function($cells) {
$cells->setBackground('#36494F')->setFontColor('#FFFFF')->setFontWeight('bold');
});
$sheet->mergeCells('A1:D1');
$sheet->mergeCells('B1:C1');
$sheet->cell('B1', function($cell) {
$cell->setValue('User List')->setBackground('#36494F');
});
$sheet->row(1, function ($row) {
$row->setBackground('#36494F')->setFontColor('#FFFFFF')->setFontWeight('bold')->setValue('User Tickets List')->setAlignment('center')->setValignment('center');
});
$sheet->row(2, function ($row) {
$row->setBackground('#FFFFFF')->setFontColor('#36494F')->setFontWeight('bold')->setAlignment('center')->setValignment('center');
});
for ($i = 3; $i <= count($userList)+1; $i++) {
$sheet->cells('A'.$i.':D'.$i, function ($cells) use ($i) {
$cells->setBackground(($i%2) == 0 ? '#DCDCDC':'#EDEDEE');
});
}
for ($ascii = ord('A'); $ascii <= ord('D'); $ascii++) {
for ($i = 1; $i <= count($userList)+1; $i++) {
$sheet->cell(chr($ascii) . $i, function ($cell) {
$cell->setBorder('thin','thin','thin','thin');
});
}
}
});
})->download('xls');
}