public static function exportCsv($cookbookTitle, $header, $rows, $filename, $pIndex, $options = []) { $options = self::_mergeOptions($options); if (file_exists($filename)) { $handle = fopen($filename, 'a+'); if (count($rows) > 0) { foreach ($rows as $row) { $row = ExcelUtil::setRowValue($row, $header, $options); fputcsv($handle, $row); unset($row); } fclose($handle); unset($header, $filename, $pIndex, $options, $handle); } } else { $objPHPExcel = new \PHPExcel(); $objPHPExcel->getActiveSheet()->setCellValue('A1', '食譜名稱'); $objPHPExcel->getActiveSheet()->setCellValue('B1', $cookbookTitle); $pIndex = $pIndex + 2; if ($options['printHeader']) { $index = 0; foreach ($header as $item) { $pCoordinate = ExcelUtil::setColumnIndex($index) . $pIndex; $objPHPExcel->setActiveSheetIndex(0)->setCellValue($pCoordinate, $item); $index++; } $pIndex = $pIndex + 1; } if (count($rows) > 0) { foreach ($rows as $row) { $row = ExcelUtil::setRowValue($row, $header, $options); $index = 0; foreach ($header as $key => $value) { $item = isset($row[$key]) ? $row[$key] : ''; $pCoordinate = ExcelUtil::setColumnIndex($index) . $pIndex; $objPHPExcel->setActiveSheetIndex(0)->setCellValue($pCoordinate, $item); $index++; } $pIndex++; } } $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV')->setDelimiter($options['delimiter'])->setEnclosure($options['quote'])->setLineEnding($options['linefeed'])->setSheetIndex(0)->setUseBOM(true)->save($filename); unset($objPHPExcel, $objWriter); } unset($rows); }