//脚本:将数据导入Excel
public function actionTest(){
$cache = \yii::$app->cache;
//获取LOCK
$lock = $cache->get(self::LOCKQA);
if($lock){
echo '任务正在执行。。。';exit;
}
$cache->set(self::LOCKQA,1);
$cacheData = $cache->get(缓存key);
if($cacheData){
$excelArray = [
'A' => ['title' => '审核日期', 'field' => 'auditime'],
'B' => ['title' => '关键词', 'field' => 'keyword'],
'C' => ['title' => '标题', 'field' => 'title'],
];
foreach($cacheData as $k => $v){
$this->clearFile($k);
foreach($v as $pk => $pv){
if($pv){
$this->qa($pv,$k,$excelArray);
}
}
}
$cacheDataNew = $cache->get(缓存key);
//求差集 当脚本在执行过程中,可能有新的任务新增 所以要求差集删除本次执行的数据
$cj = $this->array_diff_assoc_recursive($cacheDataNew,$cacheData);
if($cj){
$cache->set(缓存key,$cj);
}else{
$cache->delete(缓存key);
}
}
$cache->delete(self::LOCKQA);
}
//多维数组的差集
public function array_diff_assoc_recursive($array1,$array2){
$diffarray=array();
foreach ($array1 as $key=>$value){
//判断数组每个元素是否是数组
if(is_array($value)){
//判断第二个数组是否存在key
if(!isset($array2[$key])){
$diffarray[$key]=$value;
//判断第二个数组key是否是一个数组
}elseif(!is_array($array2[$key])){
$diffarray[$key]=$value;
}else{
$diff = $this->array_diff_assoc_recursive($value, $array2[$key]);
if($diff!=false){
$diffarray[$key]=$diff;
}
}
}elseif(!array_key_exists($key, $array2) || $value!==$array2[$key]){
$diffarray[$key]=$value;
}
}
return $diffarray;
}
//将数据导入excel多个sheet
public function qa($params,$adminid,$excelArray){
$report = new QaSearch();
$query = $report->search(['QaSearch' =>$params],3);
$exportData = [];
if($query) {
$size = 15000;
$count = $query->count();
if ($count) {
ob_start();
$cacheMethod = \PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = [' memoryCacheSize ' => '128MB'];
\PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$phpExcel = new \PHPExcel();
$phpExcel->getProperties()
->setCreator("XYWY")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$total = ceil($count / $size);
for ($i = 0; $i < $total; $i++) {
$data = $query->offset($i * $size)->limit($size)->all();
if ($data) {
foreach ($data as $k => &$v) {
$exportData[$k]['auditime'] = $v->auditime ? date('Y-m-d H:i:s', $v->auditime) : '';
$exportData[$k]['keyword'] = $v->keyword;
$exportData[$k]['title'] = $v->title;
}
$objWriter = $this->moreSheet($phpExcel, $excelArray, $exportData, $i);
unset($exportData);
unset($data);
}
}
$filename = 'test.xlsx';
unset($params);
$path = \yii::$app->params['exportFilePath'].'/';
if(!is_dir($path)){
mkdir($path);
}
$filePath = $path. $filename.'.xlsx';
$objWriter->save($filePath);
unset($phpExcel);
unset($objWriter);
}
}
unset($exportData);
}
/*@todo 生成多个sheet* */
public function moreSheet($phpExcel,$header,$data,$num){
$phpExcel->createSheet();
$phpExcel->setActiveSheetIndex($num);
// 头部
foreach ($header as $key => $val) {
$phpExcel->getActiveSheet()->setCellValue($key . '1', $val['title']);
}
// 内容
$i = 2;
if($data){
foreach ($data as $val) {
foreach ($header as $k => $v) {
$fieldValue = isset($val[$v['field']])?$val[$v['field']]:'';
$phpExcel->getActiveSheet()->setCellValue($k . $i, $fieldValue);
}
$i ++;
}
}
$objWriter = \PHPExcel_IOFactory::createWriter($phpExcel, 'Excel2007');
return $objWriter;
}
但是使用PHPExcel 加载内容过大 ,而且加载的内容很多都用不到,导出大量数据的时候会内存溢出,设置ini_set("memory_limit",'256M');