Accept Merge Request #222: (feature/hant -> develop)
Merge Request: 秒杀导出 Created By: @todayswind Accepted By: @todayswind URL: https://g-bcrc3009.coding.net/p/allocatr/d/allocatr/git/merge/222?initial=true
This commit is contained in:
commit
086cf481d8
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -766,75 +766,42 @@ class Backend extends Controller
|
||||||
return $res;
|
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);
|
set_time_limit(0);
|
||||||
|
ini_set('memory_limit', '-1');
|
||||||
ignore_user_abort(true);
|
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-Type: text/csv; charset=UTF-8');
|
||||||
header('Content-Disposition: attachment;filename="' . $filename . '"');
|
header('Content-Disposition: attachment;filename="' . $filename . '"');
|
||||||
header('Cache-Control: max-age=0');
|
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');
|
header('Pragma: public');
|
||||||
|
|
||||||
// 输出 UTF-8 BOM 防止 Excel 乱码
|
// 防止 Excel 打开乱码
|
||||||
echo "\xEF\xBB\xBF";
|
echo "\xEF\xBB\xBF";
|
||||||
|
|
||||||
$writer->save('php://output');
|
$output = fopen('php://output', 'w');
|
||||||
$spreadsheet->disconnectWorksheets();
|
|
||||||
unset($spreadsheet);
|
$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;
|
exit;
|
||||||
}
|
}
|
||||||
protected function getNestedValue(array $data, string $path, $default = '')
|
protected function getNestedValue(array $data, string $path, $default = '')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user