mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
[更新]增加CSV导出支持
This commit is contained in:
parent
217a29863b
commit
7810a9e202
@ -151,4 +151,49 @@ class ToolsService
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入CSV文件头部
|
||||
* @param string $filename 导出文件
|
||||
* @param array $headers CSV 头部(一级数组)
|
||||
*/
|
||||
public static function setCsvHeader($filename, array $headers)
|
||||
{
|
||||
header('Content-Type: application/octet-stream');
|
||||
header("Content-Disposition: attachment; filename=" . iconv('utf-8', 'gbk//TRANSLIT', $filename));
|
||||
echo @iconv('utf-8', 'gbk//TRANSLIT', '"' . implode('","', $headers) . "\"\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入CSV文件内容
|
||||
* @param array $list 数据列表(二维数组或多维数组)
|
||||
* @param array $rules 数据规则(一维数组)
|
||||
*/
|
||||
public static function setCsvBody(array $list, array $rules)
|
||||
{
|
||||
foreach ($list as $data) {
|
||||
$rows = [];
|
||||
foreach ($rules as $rule) {
|
||||
$item = self::parseKeyDot($data, $rule);
|
||||
$rows[] = $item === $data ? '' : $item;
|
||||
}
|
||||
echo @iconv('utf-8', 'gbk//TRANSLIT', "\"" . implode('","', $rows) . "\"\n");
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据数组key查询(可带点规则)
|
||||
* @param array $data 数据
|
||||
* @param string $rule 规则,如: order.order_no
|
||||
* @return mixed
|
||||
*/
|
||||
private static function parseKeyDot(array $data, $rule)
|
||||
{
|
||||
list($temp, $attr) = [$data, explode('.', trim($rule, '.'))];
|
||||
while ($key = array_shift($attr)) {
|
||||
$temp = isset($temp[$key]) ? $temp[$key] : $temp;
|
||||
}
|
||||
return (is_string($temp) || is_numeric($temp)) ? str_replace('"', '""', "\t{$temp}") : '';
|
||||
}
|
||||
|
||||
}
|
||||
|
2
vendor/autoload.php
vendored
2
vendor/autoload.php
vendored
@ -4,4 +4,4 @@
|
||||
|
||||
require_once __DIR__ . '/composer/autoload_real.php';
|
||||
|
||||
return ComposerAutoloaderInitd7f5fe6e2ec784ab7d22f916aff0bf87::getLoader();
|
||||
return ComposerAutoloaderInit25acf9996760ee7542bf8505ecd3634b::getLoader();
|
||||
|
14
vendor/composer/autoload_real.php
vendored
14
vendor/composer/autoload_real.php
vendored
@ -2,7 +2,7 @@
|
||||
|
||||
// autoload_real.php @generated by Composer
|
||||
|
||||
class ComposerAutoloaderInitd7f5fe6e2ec784ab7d22f916aff0bf87
|
||||
class ComposerAutoloaderInit25acf9996760ee7542bf8505ecd3634b
|
||||
{
|
||||
private static $loader;
|
||||
|
||||
@ -19,15 +19,15 @@ class ComposerAutoloaderInitd7f5fe6e2ec784ab7d22f916aff0bf87
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInitd7f5fe6e2ec784ab7d22f916aff0bf87', 'loadClassLoader'), true, true);
|
||||
spl_autoload_register(array('ComposerAutoloaderInit25acf9996760ee7542bf8505ecd3634b', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInitd7f5fe6e2ec784ab7d22f916aff0bf87', 'loadClassLoader'));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit25acf9996760ee7542bf8505ecd3634b', 'loadClassLoader'));
|
||||
|
||||
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
|
||||
if ($useStaticLoader) {
|
||||
require_once __DIR__ . '/autoload_static.php';
|
||||
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInitd7f5fe6e2ec784ab7d22f916aff0bf87::getInitializer($loader));
|
||||
call_user_func(\Composer\Autoload\ComposerStaticInit25acf9996760ee7542bf8505ecd3634b::getInitializer($loader));
|
||||
} else {
|
||||
$map = require __DIR__ . '/autoload_namespaces.php';
|
||||
foreach ($map as $namespace => $path) {
|
||||
@ -48,19 +48,19 @@ class ComposerAutoloaderInitd7f5fe6e2ec784ab7d22f916aff0bf87
|
||||
$loader->register(true);
|
||||
|
||||
if ($useStaticLoader) {
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInitd7f5fe6e2ec784ab7d22f916aff0bf87::$files;
|
||||
$includeFiles = Composer\Autoload\ComposerStaticInit25acf9996760ee7542bf8505ecd3634b::$files;
|
||||
} else {
|
||||
$includeFiles = require __DIR__ . '/autoload_files.php';
|
||||
}
|
||||
foreach ($includeFiles as $fileIdentifier => $file) {
|
||||
composerRequired7f5fe6e2ec784ab7d22f916aff0bf87($fileIdentifier, $file);
|
||||
composerRequire25acf9996760ee7542bf8505ecd3634b($fileIdentifier, $file);
|
||||
}
|
||||
|
||||
return $loader;
|
||||
}
|
||||
}
|
||||
|
||||
function composerRequired7f5fe6e2ec784ab7d22f916aff0bf87($fileIdentifier, $file)
|
||||
function composerRequire25acf9996760ee7542bf8505ecd3634b($fileIdentifier, $file)
|
||||
{
|
||||
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
|
||||
require $file;
|
||||
|
8
vendor/composer/autoload_static.php
vendored
8
vendor/composer/autoload_static.php
vendored
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Composer\Autoload;
|
||||
|
||||
class ComposerStaticInitd7f5fe6e2ec784ab7d22f916aff0bf87
|
||||
class ComposerStaticInit25acf9996760ee7542bf8505ecd3634b
|
||||
{
|
||||
public static $files = array (
|
||||
'1cfd2761b63b0a29ed23657ea394cb2d' => __DIR__ . '/..' . '/topthink/think-captcha/src/helper.php',
|
||||
@ -257,9 +257,9 @@ class ComposerStaticInitd7f5fe6e2ec784ab7d22f916aff0bf87
|
||||
public static function getInitializer(ClassLoader $loader)
|
||||
{
|
||||
return \Closure::bind(function () use ($loader) {
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInitd7f5fe6e2ec784ab7d22f916aff0bf87::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInitd7f5fe6e2ec784ab7d22f916aff0bf87::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInitd7f5fe6e2ec784ab7d22f916aff0bf87::$classMap;
|
||||
$loader->prefixLengthsPsr4 = ComposerStaticInit25acf9996760ee7542bf8505ecd3634b::$prefixLengthsPsr4;
|
||||
$loader->prefixDirsPsr4 = ComposerStaticInit25acf9996760ee7542bf8505ecd3634b::$prefixDirsPsr4;
|
||||
$loader->classMap = ComposerStaticInit25acf9996760ee7542bf8505ecd3634b::$classMap;
|
||||
|
||||
}, null, ClassLoader::class);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user