php 导出excel压缩包
2017-07-14 11:04
232 查看
//创建文件夹或清除文件夹下文件 $dir="var/templet"; if (!file_exists($dir)) { mkdir ($dir);//创建文件夹 }else { $dh=opendir($dir); while ($file=readdir($dh)) { if($file!="." && $file!="..") { $fullpath=$dir."/".$file; unlink($fullpath); } } closedir($dh); } //导出excel
//导出单个品类码excel $oe = new PHPExcel(); $i = 0; $x = 1; $oe->setActiveSheetIndex(0); $oe->getActiveSheet()->setTitle('品类码'.$category_code); $oe->getActiveSheet()->setCellValue('A1', '品类码:'.$category_code); $oe->getActiveSheet()->setCellValue('A2',$manage_full_name); // $oe->getActiveSheet()->setCellValue('B1', $manage_code); $oe->getActiveSheet()->getColumnDimension('A')->setWidth(50); // $oe->getActiveSheet()->getColumnDimension('B')->setWidth(50); foreach ($excel_data as $kk=>$data){ $oe->addSheet(new PHPExcel_Worksheet($oe, $kk)); $i += 1; } foreach ($excel_data as $kk=>$data){ $oe->setActiveSheetIndex($x); $column = "A"; $column++; $column++; foreach ($data as $j=>$template_field) { $column++; $oe->getActiveSheet()->setCellValue('A1', 'ERPSKU'); $oe->getActiveSheet()->setCellValue('B1', 'Account'); $oe->getActiveSheet()->setCellValue('C1', 'SellerSku'); $oe->getActiveSheet()->getColumnDimension($column)->setWidth(20); if($template_field['field_type'] == 4){ $oe->getActiveSheet()->getStyle($column)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00); } $oe->getActiveSheet()->setCellValue($column . '1', $template_field['template_name']); $oe->getActiveSheet()->getColumnDimension($column)->setWidth(20); $oe->getActiveSheet()->getComment( $column . e170 '1')->setAuthor('PHPExcel' ); //设置作者 $oe->getActiveSheet()->getComment($column .'1' )->getText()-> createTextRun($template_field['template_description']); $oe->getActiveSheet()->getComment( $column . '1' )->setWidth('150pt' )->setHeight('150pt'); }
$currentRow = 1; foreach($rowData as $rowSet=>$row){ $column2="A"; $currentColumn = 0 ; foreach ($row as $order=>$item){ // $oe->getActiveSheet()->setCellValue(chr($colbaseIndex+$currentColumn) . ($currentRow+1), $item); $oe->getActiveSheet()->setCellValue($column2 . ($currentRow+1), $item); $column2++; $currentColumn++; } $currentRow++; } $x += 1; } // var_dump($erpskus[$category_code]); // exit(); $objWriter = PHPExcel_IOFactory::createWriter($oe, "Excel2007"); $file ='var/templet/'.$line_name.'_'.$category_code.'.xlsx'; $objWriter->save($file); $oe->disconnectWorksheets(); unset($oe); //打包
//zip打包 $dir='var/templet'; $datalist = array(); if (is_dir($dir)){ $file_dir = scandir($dir); foreach($file_dir as $file){ if ($file == '.' || $file == '..'){ continue; }else{ array_push($datalist, $dir."/".$file); } } } //$datalist 就是指 导出的excel的数量,数量为1的时,则以Excel格式导出 if (count($datalist) == 1 ){ header("Location: /$datalist[0]"); exit; } //$filename = "var/templet/templet.zip"; //最终生成的文件名(含路径) $filename = "var/templet/".$line_name."_templet.zip"; //最终生成的文件名(含路径) //生成文件 $zip = new ZipArchive();//使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释 if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) { exit('无法打开文件,或者文件创建失败'); } foreach( $datalist as $val){ if(file_exists($val)){ $zip->addFile( $val, basename($val));//第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下 } } $zip->close();//关闭 if(!file_exists($filename)){ exit("无法找到文件"); //即使创建,仍有可能失败。。。。 } header("Location: /$filename"); exit; // end zip打包
相关文章推荐
- .php导出excel(多种方法)
- php导出数据到excel,防止身份证等数字字符格式变成科学计数的方法
- php导出excel
- PHP导入导出Excel方法
- PhpExcel导出excel报错:net::ERR_INVALID_RESPONSE
- 超实用的PHPExcel[导入][导出]实现方法总结
- PHPExcel导出xls
- php 利用 PHPExcel 导出 导入 Excel 方法介绍(功能介绍)
- ThinkPHP3.2+PHPExcel1.8版类库 实现导入导出excel表
- PHPExcel 大数据的导出
- phpexcel 导入导出
- php导出Excel文件
- php 导出excel 很简单很好用
- PHPExcel 在LINUX环境中导出乱码的问题解决
- PHPEXCEL精简版之导出导入
- PHP原生代码导出EXCEL功能
- ci框架整合phpexcel,实现数据导出excel
- PHPExcel导出excel
- phpExcel导出excel加超级链接的实例代码[转]
- PHPExcel导出格式错误问题--格式与文件扩展名格式不一致