<?php
namespace api\service;
class ExportService
{
public static $outPutFile = '';
* 导出文件
* @param string $fileName
* @param $data
* @param array $formFields
* @return mixed
*/
public static function exportData($fileName = '', $data, $formFields = [])
{
$fileArr = [];
$tmpPath = \Yii::$app->params['excelSavePath'];
foreach (array_chunk($data, 10000) as $key => $value) {
self::$outPutFile = '';
$subject = !empty($fileName) ? $fileName : 'data_';
$subject .= date('YmdHis');
if (empty($value) || empty($formFields)) {
continue;
}
self::$outPutFile = $tmpPath . $subject . $key . '.csv';
if (!file_exists(self::$outPutFile)) {
touch(self::$outPutFile);
}
$index = array_keys($formFields);
$header = array_values($formFields);
self::outPut($header);
foreach ($value as $k => $v) {
$tmpData = [];
foreach ($index as $item) {
$tmpData[] = isset($v[$item]) ? $v[$item] : '';
}
self::outPut($tmpData);
}
$fileArr[] = self::$outPutFile;
}
$zipFile = $tmpPath . $fileName . date('YmdHi') . '.zip';
$zipRes = self::zipFile($fileArr, $zipFile);
return $zipRes;
}
* 向文件写入数据
* @param array $data
*/
public static function outPut($data = [])
{
if (is_array($data) && !empty($data)) {
$data = implode(',', $data);
file_put_contents(self::$outPutFile, iconv("UTF-8", "GB2312//IGNORE", $data) . PHP_EOL, FILE_APPEND);
}
}
* 压缩文件
* @param $sourceFile
* @param $distFile
* @return mixed
*/
public static function zipFile($sourceFile, $distFile)
{
$zip = new \ZipArchive();
if ($zip->open($distFile, \ZipArchive::CREATE) !== true) {
return $sourceFile;
}
$zip->open($distFile, \ZipArchive::CREATE);
foreach ($sourceFile as $file) {
$fileContent = file_get_contents($file);
$file = iconv('utf-8', 'GBK', basename($file));
$zip->addFromString($file, $fileContent);
}
$zip->close();
return $distFile;
}
* 下载文件
* @param $filePath
* @param $fileName
*/
public static function download($filePath, $fileName)
{
if (!file_exists($filePath . $fileName)) {
header('HTTP/1.1 404 NOT FOUND');
} else {
$file = fopen($filePath . $fileName, "rb");
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: " . filesize($filePath . $fileName));
Header("Content-Disposition: attachment; filename=" . $fileName);
echo fread($file, filesize($filePath . $fileName));
fclose($file);
exit();
}
}
}
评论