Accept Merge Request #221: (feature/hant -> develop)
Merge Request: 导出逻辑 Created By: @todayswind Accepted By: @todayswind URL: https://g-bcrc3009.coding.net/p/allocatr/d/allocatr/git/merge/221?initial=true
This commit is contained in:
commit
e2e60c43b8
|
|
@ -1139,7 +1139,7 @@ class Order extends Backend
|
||||||
return $result->getData()['rows'] ?? [];
|
return $result->getData()['rows'] ?? [];
|
||||||
};
|
};
|
||||||
|
|
||||||
$this->exportExcelByStream($columns, $fetchData, '订单导出_' . date('Ymd_His') . '.xlsx');
|
$this->exportCsvByStream($columns, $fetchData, '订单导出_' . date('Ymd_His') . '.xlsx');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ namespace app\common\controller;
|
||||||
use app\admin\library\Auth;
|
use app\admin\library\Auth;
|
||||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
use PhpOffice\PhpSpreadsheet\Writer\Csv;
|
||||||
use think\Config;
|
use think\Config;
|
||||||
use think\Controller;
|
use think\Controller;
|
||||||
use think\Db;
|
use think\Db;
|
||||||
|
|
@ -766,12 +766,10 @@ class Backend extends Controller
|
||||||
return $res;
|
return $res;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function exportExcelByStream(array $columns, callable $fetchData, string $filename = '导出.xlsx', int $limit = 1000)
|
protected function exportCsvByStream(array $columns, callable $fetchData, string $filename = '导出.csv', int $limit = 1000)
|
||||||
{
|
{
|
||||||
$page = 1;
|
$page = 1;
|
||||||
$rowIndex = 2;
|
$rowIndex = 2;
|
||||||
if (ob_get_level()) ob_end_clean();
|
|
||||||
ob_clean();
|
|
||||||
|
|
||||||
ini_set('memory_limit', '512M');
|
ini_set('memory_limit', '512M');
|
||||||
set_time_limit(0);
|
set_time_limit(0);
|
||||||
|
|
@ -786,8 +784,11 @@ class Backend extends Controller
|
||||||
$spreadsheet->setActiveSheetIndex(0);
|
$spreadsheet->setActiveSheetIndex(0);
|
||||||
$sheet = $spreadsheet->getActiveSheet();
|
$sheet = $spreadsheet->getActiveSheet();
|
||||||
|
|
||||||
$writer = new Xlsx($spreadsheet);
|
$writer = new Csv($spreadsheet);
|
||||||
$writer->setPreCalculateFormulas(false);
|
$writer->setDelimiter(','); // 可改为 \t 导出为 TSV
|
||||||
|
$writer->setEnclosure('"');
|
||||||
|
$writer->setLineEnding("\r\n");
|
||||||
|
$writer->setSheetIndex(0);
|
||||||
|
|
||||||
// 获取字段映射
|
// 获取字段映射
|
||||||
$titles = array_column($columns, 'title');
|
$titles = array_column($columns, 'title');
|
||||||
|
|
@ -818,15 +819,19 @@ class Backend extends Controller
|
||||||
$page++;
|
$page++;
|
||||||
} while ($count === $limit);
|
} while ($count === $limit);
|
||||||
|
|
||||||
// 输出为下载流
|
// 输出为 CSV 下载流
|
||||||
$filename = $filename ?: ('导出_' . date('Ymd_His') . '.xlsx');
|
$filename = $filename ?: ('导出_' . date('Ymd_His') . '.csv');
|
||||||
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
||||||
|
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('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
|
||||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||||
header('Pragma: public');
|
header('Pragma: public');
|
||||||
|
|
||||||
|
// 输出 UTF-8 BOM 防止 Excel 乱码
|
||||||
|
echo "\xEF\xBB\xBF";
|
||||||
|
|
||||||
$writer->save('php://output');
|
$writer->save('php://output');
|
||||||
$spreadsheet->disconnectWorksheets();
|
$spreadsheet->disconnectWorksheets();
|
||||||
unset($spreadsheet);
|
unset($spreadsheet);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user