diff --git a/application/admin/controller/Order.php b/application/admin/controller/Order.php index b2f51a8..e4f9b3e 100644 --- a/application/admin/controller/Order.php +++ b/application/admin/controller/Order.php @@ -1191,44 +1191,5 @@ class Order extends Backend } - protected function streamCsv(array $columns, \Generator $rows, string $filename) - { - set_time_limit(0); - ini_set('memory_limit', '-1'); - ignore_user_abort(true); - - header('Content-Type: text/csv; charset=UTF-8'); - header('Content-Disposition: attachment;filename="' . $filename . '"'); - header('Cache-Control: max-age=0'); - header('Pragma: public'); - - // 防止 Excel 打开乱码 - echo "\xEF\xBB\xBF"; - - $output = fopen('php://output', 'w'); - - $fields = array_column($columns, 'field'); - $titles = array_column($columns, 'title'); - - // 写入表头 - fputcsv($output, $titles); - - // 逐行写入数据 - foreach ($rows as $row) { - $line = []; - foreach ($fields as $field) { - $value = $this->getNestedValue($row, $field); - if (is_array($value) || is_object($value)) { - $value = json_encode($value, JSON_UNESCAPED_UNICODE); - } - $line[] = $value; - } - fputcsv($output, $line); - } - - fclose($output); - exit; - } - } diff --git a/application/common/controller/Backend.php b/application/common/controller/Backend.php index 1357357..da56d80 100755 --- a/application/common/controller/Backend.php +++ b/application/common/controller/Backend.php @@ -766,75 +766,42 @@ class Backend extends Controller return $res; } - protected function exportCsvByStream(array $columns, callable $fetchData, string $filename = '导出.csv', int $limit = 1000) + protected function streamCsv(array $columns, \Generator $rows, string $filename) { - $page = 1; - $rowIndex = 2; - - ini_set('memory_limit', '512M'); set_time_limit(0); + ini_set('memory_limit', '-1'); ignore_user_abort(true); - ini_set('zlib.output_compression', 'Off'); - - // 创建 Spreadsheet 和 Sheet - $spreadsheet = new Spreadsheet(); - $spreadsheet->removeSheetByIndex(0); - $sheet = new Worksheet($spreadsheet, 'Sheet1'); - $spreadsheet->addSheet($sheet); - $spreadsheet->setActiveSheetIndex(0); - $sheet = $spreadsheet->getActiveSheet(); - - $writer = new Csv($spreadsheet); - $writer->setDelimiter(','); // 可改为 \t 导出为 TSV - $writer->setEnclosure('"'); - $writer->setLineEnding("\r\n"); - $writer->setSheetIndex(0); - - // 获取字段映射 - $titles = array_column($columns, 'title'); - $fields = array_column($columns, 'field'); - - // 写入表头 - $sheet->fromArray($titles, null, 'A1'); - - // 分页写入数据 - do { - $rows = $fetchData($page, $limit); - $count = count($rows); - if ($count === 0) break; - - foreach ($rows as $row) { - $dataRow = []; - foreach ($fields as $field) { - $value = $this->getNestedValue($row->toArray(), $field); - if (is_array($value) || is_object($value)) { - $value = json_encode($value, JSON_UNESCAPED_UNICODE); - } - $dataRow[] = $value; - } - $sheet->fromArray($dataRow, null, 'A' . $rowIndex); - $rowIndex++; - } - - $page++; - } while ($count === $limit); - - // 输出为 CSV 下载流 - $filename = $filename ?: ('导出_' . date('Ymd_His') . '.csv'); header('Content-Type: text/csv; charset=UTF-8'); header('Content-Disposition: attachment;filename="' . $filename . '"'); header('Cache-Control: max-age=0'); - header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); - header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Pragma: public'); - // 输出 UTF-8 BOM 防止 Excel 乱码 + // 防止 Excel 打开乱码 echo "\xEF\xBB\xBF"; - $writer->save('php://output'); - $spreadsheet->disconnectWorksheets(); - unset($spreadsheet); + $output = fopen('php://output', 'w'); + + $fields = array_column($columns, 'field'); + $titles = array_column($columns, 'title'); + + // 写入表头 + fputcsv($output, $titles); + + // 逐行写入数据 + foreach ($rows as $row) { + $line = []; + foreach ($fields as $field) { + $value = $this->getNestedValue($row, $field); + if (is_array($value) || is_object($value)) { + $value = json_encode($value, JSON_UNESCAPED_UNICODE); + } + $line[] = $value; + } + fputcsv($output, $line); + } + + fclose($output); exit; } protected function getNestedValue(array $data, string $path, $default = '')