使用前请先安装 phpoffice/phpexcel 安装命令:composer require phpoffice/phpexcel
namespace App\Tools;
use Exception;
use PHPExcel;
use PHPExcel_CachedObjectStorageFactory;
use PHPExcel_Cell_DataValidation;
use PHPExcel_Exception;
use PHPExcel_IOFactory;
use PHPExcel_Reader_Exception;
use PHPExcel_Settings;
use PHPExcel_Style_Alignment;
use PHPExcel_Style_Fill;
use PHPExcel_Worksheet;
use PHPExcel_Writer_Exception;
use Swoft\Log\Log;
class ExcelTpl
{
private $objExcel;
private $title;
private $filePath = "/var/cache/nginx/demo/";
public function handle(string $title, array $fieldData, array $dataInfo = [], array $userInfo = [])
{
ini_set('memory_limit','4096M');
$this->title = $title;
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
$cacheSettings = array('memoryCacheSize'=>'15MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
$objSheet = $this->getHandle();
$this->setHeader($objSheet, $fieldData);
$this->setData($objSheet, $fieldData, $dataInfo);
empty($userInfo) ? $this->outPut() : (new SendEmail())->handle($userInfo, [
'title' => $title,
'content' => "您导出的数据为 $title 请及时下载",
'attachment' => $this->saveFile()
]);
}
protected function getHandle(): PHPExcel_Worksheet
{
$this->objExcel = new PHPExcel();
return $this->objExcel->getActiveSheet();
}
protected function setHeader(PHPExcel_Worksheet $objSheet, array $fieldData)
{
$objSheet->setTitle($this->title);
$this->objExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->objExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$pCoordinate = '';
$isFontColor = false;
foreach ($fieldData as $fieldDatum) {
$pCoordinate = $fieldDatum['pColumn'] . '1';
$objSheet->setCellValue($pCoordinate, $fieldDatum['pValue']);
$objSheet->getColumnDimension($fieldDatum['pColumn'])->setWidth($fieldDatum['width'] ?? 20);
if (!empty($fieldDatum['comment'])) {
$objSheet->getComment($pCoordinate)->getText()->createTextRun($fieldDatum['comment']);
}
if (!empty($fieldDatum['fontColor'])) {
$objSheet->getStyle($pCoordinate)->getFont()->getColor()->setRGB($fieldDatum['fontColor']);
$isFontColor = true;
}
if (!empty($fieldDatum['dateFormat'])) {
$objSheet->getStyle($fieldDatum['pColumn'])->getNumberFormat()->setFormatCode($fieldDatum['dateFormat']);
}
if (!empty($fieldDatum['select'])) {
$selectStr = implode(',', $fieldDatum['select']);
foreach ($fieldDatum['select'] as $key => $val) {
$objSheet->getCell($fieldDatum['pColumn'] . ($key + 2))->getDataValidation()
->setType(PHPExcel_Cell_DataValidation::TYPE_LIST)
->setErrorStyle(PHPExcel_Cell_DataValidation::STYLE_INFORMATION)
->setAllowBlank(false)
->setShowInputMessage(true)
->setShowErrorMessage(true)
->setShowDropDown(true)
->setErrorTitle('输入的值有误')
->setError('您输入的值不在下拉框列表内(请不要自己填写,应在下拉列表中选择)')
->setPromptTitle()
->setPrompt()
->setFormula1('"' . $selectStr . '"');
}
}
}
if (!$isFontColor) {
$objSheet->getStyle('A1:' . $pCoordinate)->getFont()->getColor()->setRGB('FFFFFF');
$objSheet->getStyle('A1:' . $pCoordinate)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('595959');
}
$objSheet->getStyle('A1:' . $pCoordinate)->getFont()->setSize(11)->setBold(true);
$objSheet->getStyle('A1:' . $pCoordinate)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
}
protected function setData(PHPExcel_Worksheet $objSheet, array $fieldData, array $dataInfo): void
{
if (empty($dataInfo)) {
return;
}
foreach ($dataInfo as $key => $value) {
$key += 2;
foreach ($fieldData as $fieldKey => $fieldDatum) {
if (!empty($fieldDatum['dateType'])) {
$objSheet->setCellValueExplicit($fieldDatum['pColumn'] . $key, $value[$fieldKey], $fieldDatum['dateType']);
continue;
}
$objSheet->setCellValue($fieldDatum['pColumn'] . $key, $value[$fieldKey]);
}
}
}
protected function outPut()
{
$fileName = $this->title . date('YmdHis ') . '.xlsx';
$objWriter = PHPExcel_IOFactory::createWriter($this->objExcel, 'Excel2007');
ob_start();
$objWriter->save('php://output');
$res = response()->getSwooleResponse();
$res->header('Content-Type', 'application/vnd.ms-excel');
$res->header('Content-Disposition', 'attachment;filename=' . $fileName);
$res->header('Cache-Control', 'max-age=0');
$res->write(ob_get_clean());
}
protected function saveFile(): string
{
if (!file_exists($this->filePath)) {
mkdir($this->filePath, 0777, true);
}
$path = $this->filePath . $this->title . '.xlsx';
try {
$objWriter = PHPExcel_IOFactory::createWriter($this->objExcel, 'Excel2007');
$objWriter->save($path);
return $path;
} catch (Exception $e) {
Log::error(date("Y-m-d H:i:s") . '--' . $path . '--文件保存失败', [$e->getMessage(), $e->getFile(), $e->getLine()]);
return false;
}
}
public function numToExcelLetter(int $num): string
{
$result = '';
while ($num > 0) {
$mod = $num % 26;
$num = (int)($num / 26);
if ($mod == 0) {
$num--;
$temp = 'Z' . $result;
} else {
$temp = chr(64 + $mod) . $result;
}
$result = $temp;
}
return $result;
}
}
|