布景
针对PHP导出Excel的优化,正在我以前的一篇文章里曾经做过引见:对于PHP内存溢出的考虑,本文次要是引见一款高功能的导出组件–xlswriter,他是一个PHP C扩大,民间文档地点,请点击。
保举:PHP视频教程
装置
装置pecl
当咱们发现pecl未装置时,则需求装置pecl。普通状况下,是装置正在PHP的装置目次,示例饬令以下:
# 进入PHP装置目次 cd /usr/local/php/bin curl -o go-pear.php http://pear.php.net/go-pear.phar php go-pear.php # 装置实现后,软衔接到bin目次下 ln -s /usr/local/php/bin/pecl /usr/bin/pecl
装置xlswriter
pecl install xlswriter # 增加 extension = xlswriter.so 到 ini 设置装备摆设
应用
详细应用能够参考民间文档,会引见的愈加具体,我这就上一段我应用中的代码:
封装的导出service
/**
* 下载
* @param $header
* @param $data
* @param $fileName
* @param $type
* @return bool
* @throws
*/
public function download($header, $data, $fileName)
{
$config = [
'path' => $this->getTmpDir() . '/',
];
$now = date('YmdHis');
$fileName = $fileName . $now . '.xlsx';
$xlsxObject = new \Vtiful\Kernel\Excel($config);
// Init File
$fileObject = $xlsxObject->fileName($fileName);
// 设置款式
$fileHandle = $fileObject->getHandle();
$format = new \Vtiful\Kernel\Format($fileHandle);
$style = $format->bold()->background(
\Vtiful\Kernel\Format::COLOR_YELLOW
)->align(Format::FORMAT_ALIGN_VERTICAL_CENTER)->toResource();
// Writing data to a file ......
$fileObject->header($header)
->data($data)
->freezePanes(1, 0)
->setRow('A1', 20, $style);
// Outptu
$filePath = $fileObject->output();
// 下载
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Disposition: attachment;filename="' . $fileName . '"');
header('Cache-Control: max-age=0');
header('Content-Length: ' . filesize($filePath));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
if (copy($filePath, 'php://output') === false) {
throw new RuntimeException('导出失败');
}
// Delete temporary file
@unlink($filePath);
return true;
}
/**
* 猎取暂时文件夹
* @return false|string
*/
private function getTmpDir()
{
// 目次能够自界说
// return \Yii::$app->params['downloadPath'];
$tmp = ini_get('upload_tmp_dir');
if ($tmp !== False && file_exists($tmp)) {
return realpath($tmp);
}
return realpath(sys_get_temp_dir());
}
/**
* 读取文件
* @param $path
* @param $fileName
* @return array
*/
public function readFile($path,$fileName)
{
// 读取测试文件
$config = ['path' => $path];
$excel = new \Vtiful\Kernel\Excel($config);
$data = $excel->openFile($fileName)
->openSheet()
->getSheetData();
return $data;
}挪用处代码
导出
/**
* 导出
*/
public function actionExport()
{
try {
/**
* @var $searchModel SkuBarCodeSearch
*/
$searchModel = Yii::createObject(SkuBarCodeSearch::className());
$queryParams['SkuBarCodeSearch'] = [];
$result = $searchModel->search($queryParams, true);
$formatData = [];
if (!empty($result)) {
foreach ($result as $key => &$value) {
$tmpData = [
'sku_code' => $value['sku_code'],
'bar_code' => $value['bar_code'],
'created_at' => $value['created_at'],
'updated_at' => $value['updated_at'],
];
$formatData[] = array_values($tmpData);
}
unset($value);
}
$fields = [
'sku_code' => 'SKU编码',
'bar_code' => '条形码',
'created_at' => '创立工夫',
'updated_at' => '更新工夫',
];
/**
* @var $utilService UtilService
*/
$utilService = UtilService::getInstance();
$utilService->download(array_values($fields), $formatData, 'sku_single_code');
} catch (\Exception $e) {
Yii::$app->getSession()->setFlash('error', '导出失败');
}
}导入
public function actionImportTmpSku()
{
try {
/**
* @var $utilService UtilService
*/
$utilService = UtilService::getInstance();
$path = '/tmp/'; // 文件目次
$fileName = 'sku_0320.xlsx';
$excelData = $utilService->readFile($path, $fileName);
unset($excelData[0]);
$excelData = array_merge($excelData);
// ... ... 营业代码
} catch (\Exception $e) {
echo $e->getMessage();
exit;
}
}论断
全体应用上去,正在解决年夜数据量时,功能相比于原来的PHPExcel的确高了不少。
本文系转载,原文地点是:
https://tsmliyun.github.io/php/PHP%E5%AF%BC%E5%87%BAExcel%E7%9A%84%E4%BC%98%E5%8C%96/
以上就是对于PHP导出Excel的优化详解的具体内容,更多请存眷资源魔其它相干文章!
标签: php php开发教程 php开发资料 php开发自学
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!