101 lines
3.3 KiB
PHP
101 lines
3.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @author Any
|
|
* @description KISS
|
|
* @date 2021年9月1日
|
|
* @version 1.0.0
|
|
*
|
|
* _____LOG_____
|
|
*
|
|
*/
|
|
namespace app\components;
|
|
|
|
|
|
use PHPExcel;
|
|
use PHPExcel_IOFactory;
|
|
|
|
|
|
class ExportFile
|
|
{
|
|
public static function getSheetColIndex($index)
|
|
{
|
|
$letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
if($index <= 26){
|
|
return $letters[$index - 1];
|
|
}
|
|
if($index <= 26 * 26){
|
|
$int = floor($index / 26);
|
|
$remainder = $index % 26;
|
|
return $letters[$int - 1] . $letters[$remainder - 1];
|
|
}
|
|
throw new \Exception("超出范围");
|
|
}
|
|
|
|
/**
|
|
* @param array $data $data['title']工作表标题,$data['list']工作表数据
|
|
* @param string $filename 文件保存路径
|
|
* @param string $style 工作表样式
|
|
* @param string $author 作者信息
|
|
*/
|
|
public static function xlsx($data, $filename, $style="default", $author="Any")
|
|
{
|
|
//引入Excel类
|
|
$objPHPExcel = new \PHPExcel();
|
|
// 设置excel文档的属性
|
|
$objPHPExcel->getProperties()->setCreator($author)
|
|
->setLastModifiedBy($author)
|
|
->setTitle($author)
|
|
->setSubject($author)
|
|
->setDescription($author)
|
|
->setKeywords($author)
|
|
->setCategory($author);
|
|
//设置excel工作表名及文件名
|
|
$title = $data['title'];
|
|
$objPHPExcel->getActiveSheet()->setTitle($title);
|
|
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
|
|
if(!isset($data['list'])){
|
|
throw new \Exception("缺少数据");
|
|
}
|
|
foreach ($data['list'] as $index => $item){
|
|
$rowIndex = $index + 1;
|
|
for($k=0; $k < count($item); $k++){
|
|
$pos = self::getSheetColIndex($k+1).$rowIndex;
|
|
if($style == "default"){
|
|
$objPHPExcel->getActiveSheet()->setCellValue($pos,$item[$k]);
|
|
if($rowIndex == 1){
|
|
$objPHPExcel->getActiveSheet()->getStyle($pos)->getFont()->setSize(10)->setBold(true);
|
|
$objPHPExcel->getActiveSheet()->getStyle($pos)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
|
}
|
|
} else {
|
|
$objPHPExcel->getActiveSheet()->setCellValue($pos,$item[$k]);
|
|
}
|
|
}
|
|
}
|
|
$exportPath = dirname($filename);
|
|
$exportPath = trim($exportPath,".");
|
|
$exportPath = trim($exportPath,"/");
|
|
if(!file_exists($exportPath) && strlen($exportPath)){
|
|
self::mkDir($exportPath);
|
|
}
|
|
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
|
|
$objWriter->save($filename);
|
|
if(file_exists($filename)){
|
|
return $filename;
|
|
}
|
|
throw new \Exception("文件导出失败");
|
|
}
|
|
|
|
public static function csv()
|
|
{
|
|
|
|
}
|
|
|
|
//目录创建(递归创建)
|
|
public static function mkDir($dir, $mode = 0777)
|
|
{
|
|
return is_dir($dir) or self::mkDir(dirname($dir), $mode) and mkdir($dir, $mode);
|
|
}
|
|
}
|
|
|