太复杂了,参考下这个标准范例吧,不要直接输出,用文件接受
$objPHPExcel = new \PHPExcel();
$sheet = $objPHPExcel->setActiveSheetIndex(0);
$titleExcelss = [
[
'Handle',
'Title',
'Body (HTML)',
'Vendor',
'Type',
'Tags',
'Published',
'Option1 Name',
'Option1 Value',
'Option2 Name',
'Option2 Value',
'Option3 Name',
'Option3 Value',
'Variant SKU',
'Variant Grams',
'Variant Inventory Tracker',
'Variant Inventory Qty',
'Variant Inventory Policy',
'Variant Fulfillment Service',
'Variant Price',
'Variant Compare At Price',
'Variant Requires Shipping',
'Variant Taxable',
'Variant Barcode',
'Image Src',
'Image Position',
'Image Alt Text',
'Gift Card',
'SEO Title',
'SEO Description',
'Google Shopping / Google Product Category',
'Google Shopping / Gender',
'Google Shopping / Age Group',
'Google Shopping / MPN',
'Google Shopping / AdWords Grouping',
'Google Shopping / AdWords Labels',
'Google Shopping / Condition',
'Google Shopping / Custom Product',
'Google Shopping / Custom Label 0',
'Google Shopping / Custom Label 1',
'Google Shopping / Custom Label 2',
'Google Shopping / Custom Label 3',
'Google Shopping / Custom Label 4',
'Variant Image',
'Variant Weight Unit',
'Variant Tax Code',
'Cost per item',
'Status'
]
];
$count = count($titleExcelss[0]);
$sheet->getStyle('A1:AV1')->getFont()->setSize(12);//字体大小
foreach ($titleExcelss as $ki => $node) {
$k = 'A';
foreach ($node as $one) {
$sheet->setCellValue($k . strval($ki + 1), $one);
#$sheet->getStyle('A1:AV1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置文字居中
$sheet->getColumnDimension($k)->setWidth(25);#设置列的宽度
$k++;
}
}
$startRow = 1;
foreach ($new_list_array as $ki => $node) {
$startRow++;
$m = 'A';
for ($i = 0; $i < $count; $i++) {
$sheet->setCellValue($m . $startRow, $node[$i] ?? 0);
$m++;
}
}
$path = ROOT_PATH . 'goods' . rand(1, 3333333333333) . '.xls';
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($path);