Commit 3eb1ef2e authored by 王源's avatar 王源 🎧

修改导出通用文件

parent 499582ab
...@@ -9,13 +9,14 @@ ...@@ -9,13 +9,14 @@
namespace Meibuyu\Micro\Tools; namespace Meibuyu\Micro\Tools;
use Hyperf\Contract\ConfigInterface;
use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Protection; use PhpOffice\PhpSpreadsheet\Style\Protection;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class Exporter class Exporter
{ {
/** /**
...@@ -37,10 +38,7 @@ class Exporter ...@@ -37,10 +38,7 @@ class Exporter
CONST EXPORTER_TYPE_XLS = 2; CONST EXPORTER_TYPE_XLS = 2;
CONST EXPORTER_TYPE_XLSX = 3; CONST EXPORTER_TYPE_XLSX = 3;
/** private $rootPath;
* @var string 设置非保护区间 例如"A1:B1";从A列B列第一行到数据结束行不需要保护
*/
private $unprotectRange = "";
private $beginRowIndex = 0; private $beginRowIndex = 0;
private $fileType = 'Xlsx'; private $fileType = 'Xlsx';
private $isFromTemplate = false; private $isFromTemplate = false;
...@@ -50,47 +48,54 @@ class Exporter ...@@ -50,47 +48,54 @@ class Exporter
private $name = ""; private $name = "";
private $beginColumnChar = "A"; private $beginColumnChar = "A";
/**
* @var ConfigInterface
*/
protected $config;
/** /**
* Exporter constructor. * Exporter constructor.
* @param $export_type 导出者类型 支持 Exporter::EXPORTER_TYPE_CSV Exporter::EXPORTER_TYPE_XLS Exporter::EXPORTER_TYPE_XLSX * @param int $export_type 类型
* @param int $sheetIndex sheet索引 * @param string $tempFilePath 模板文件地址
* @param string $name $sheet表 * @param string $name 名称
* @param string $fromTemplateFile 从模板中创建 导出者 * @param int $sheetIndex
* @throws Exception * @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
* @throws \Exception
*/ */
public function __construct($export_type, $sheetIndex = 0, $name = "export_data", $fromTemplateFile = "") public function __construct(int $export_type, $tempFilePath = "", $name = "export_data", $sheetIndex = 0)
{ {
$reader = null; $this->config = container(ConfigInterface::class);
switch ($export_type) { $this->rootPath = $this->config->get('server.settings.document_root', BASE_PATH . '/public/');
case self::EXPORTER_TYPE_CSV: if ($tempFilePath) {
$this->fileType = "Csv"; $tempFilePath = $this->rootPath . $tempFilePath;
break; if (file_exists($tempFilePath)) {
case self::EXPORTER_TYPE_XLS: $this->fileType = ucfirst(strtolower(pathinfo($tempFilePath, PATHINFO_EXTENSION)));
$this->fileType = "Xls"; $reader = IOFactory::createReader($this->fileType)->load($tempFilePath);
break; $this->isFromTemplate = true;
case self::EXPORTER_TYPE_XLSX: } else {
$this->fileType = "Xlsx"; throw new \Exception("模板文件不存在");
break; }
default: } else {
throw new Exception("类型不支持。"); switch ($export_type) {
break; case self::EXPORTER_TYPE_CSV:
} $this->fileType = "Csv";
if ($fromTemplateFile && !file_exists($fromTemplateFile)) { break;
throw new Exception("模板文件不存在。"); case self::EXPORTER_TYPE_XLS:
$this->fileType = ucfirst(strtolower(pathinfo($fromTemplateFile, PATHINFO_EXTENSION))); $this->fileType = "Xls";
$reader = IOFactory::createReaderForFile($fromTemplateFile); break;
$reader = $reader->load($fromTemplateFile); case self::EXPORTER_TYPE_XLSX:
$this->isFromTemplate = true; $this->fileType = "Xlsx";
} break;
if (!$this->isFromTemplate && empty($reader)) { default:
$reader = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); throw new \Exception("类型不支持。");
break;
}
$reader = new Spreadsheet();
} }
$this->name = $name; $this->name = $name;
$sheet = $reader->getSheet($sheetIndex); $this->sheet = $reader->getSheet($sheetIndex);
$sheet->setTitle($name);
$this->reader = $reader; $this->reader = $reader;
$this->sheet = $sheet;
$this->sheetIndex = $sheetIndex; $this->sheetIndex = $sheetIndex;
} }
...@@ -101,7 +106,6 @@ class Exporter ...@@ -101,7 +106,6 @@ class Exporter
*/ */
public function setUnprotectRange($unprotectRange = "") public function setUnprotectRange($unprotectRange = "")
{ {
$this->unprotectRange = $this->unprotectRange;
$this->sheet->getProtection()->setSheet(true); $this->sheet->getProtection()->setSheet(true);
if ($unprotectRange) { if ($unprotectRange) {
$this->sheet->getStyle($unprotectRange) $this->sheet->getStyle($unprotectRange)
...@@ -170,7 +174,7 @@ class Exporter ...@@ -170,7 +174,7 @@ class Exporter
} }
/** /**
* 根据名字sheet索引激化切换到 该表 * 根据sheet索引切换到对应sheet
* @param int $sheetIndex * @param int $sheetIndex
* @throws \PhpOffice\PhpSpreadsheet\Exception * @throws \PhpOffice\PhpSpreadsheet\Exception
*/ */
...@@ -191,12 +195,12 @@ class Exporter ...@@ -191,12 +195,12 @@ class Exporter
*/ */
public function append(array $data, $keys = []) public function append(array $data, $keys = [])
{ {
// 一维数组转二维
//一维数组转二维
if (!(isset($data[0]) && is_array($data[0]))) { if (!(isset($data[0]) && is_array($data[0]))) {
$data = [$data]; $data = [$data];
} }
if ($keys) {
if (count($keys)) {
foreach ($data as $k => $v) { foreach ($data as $k => $v) {
$data[$k] = $this->getKeyValue($v, $keys); $data[$k] = $this->getKeyValue($v, $keys);
} }
...@@ -204,11 +208,12 @@ class Exporter ...@@ -204,11 +208,12 @@ class Exporter
$this->sheet->fromArray($data, null, $this->beginColumnChar . $this->beginRowIndex); $this->sheet->fromArray($data, null, $this->beginColumnChar . $this->beginRowIndex);
//美化样式 //美化样式
$this->applyStyle($this->beginColumnChar . $this->beginRowIndex . ":" . $this->sheet->getHighestColumn() . ($this->beginRowIndex + count($data))); $this->applyStyle($this->beginColumnChar . $this->beginRowIndex . ":" . $this->sheet->getHighestColumn() . ($this->beginRowIndex + count($data) - 1));
$this->beginRowIndex += count($data); $this->beginRowIndex += count($data);
} }
/**根据key提取数据 /**
* 根据key提取数据
* @param array $data * @param array $data
* @param array $keys * @param array $keys
* @return array * @return array
...@@ -271,11 +276,12 @@ class Exporter ...@@ -271,11 +276,12 @@ class Exporter
} }
/** /**
*下载导出的文件 * 下载导出的文件
* @param int $downloadType 下载形式 支持 Exporter::DOWNLOAD_TYPE_STREAM Exporter::DOWNLOAD_TYPE_RETURN_FILE_PATH Exporter::DOWNLOAD_TYPE_SAVE_AND_DOWNLOAD * @param int $downloadType 下载形式 支持 Exporter::DOWNLOAD_TYPE_STREAM Exporter::DOWNLOAD_TYPE_RETURN_FILE_PATH Exporter::DOWNLOAD_TYPE_SAVE_AND_DOWNLOAD
* @param string $filename 下载的文件名 * @param string $filename 下载的文件名
* @return string * @return mixed | void
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
* @throws \Exception
*/ */
public function download(int $downloadType, $filename = "") public function download(int $downloadType, $filename = "")
{ {
...@@ -284,11 +290,11 @@ class Exporter ...@@ -284,11 +290,11 @@ class Exporter
$filename = $this->name ? $this->name : rand(1, 9999999) . time() . rand(1, 9999999); $filename = $this->name ? $this->name : rand(1, 9999999) . time() . rand(1, 9999999);
} }
$filename .= "." . strtolower($this->fileType); $filename .= "." . strtolower($this->fileType);
$this->reader->setActiveSheetIndex(0);
$objWriter = IOFactory::createWriter($this->reader, $this->fileType); $objWriter = IOFactory::createWriter($this->reader, $this->fileType);
switch ($downloadType) { switch ($downloadType) {
case self::DOWNLOAD_TYPE_STREAM: case self::DOWNLOAD_TYPE_STREAM:
case self::DOWNLOAD_TYPE_SAVE_AND_DOWNLOAD: case self::DOWNLOAD_TYPE_SAVE_AND_DOWNLOAD:
if ($this->fileType == 'Xlsx') { if ($this->fileType == 'Xlsx') {
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
} elseif ($this->fileType == 'Xls') { } elseif ($this->fileType == 'Xls') {
...@@ -304,7 +310,7 @@ class Exporter ...@@ -304,7 +310,7 @@ class Exporter
if ($downloadType == self::DOWNLOAD_TYPE_STREAM) { if ($downloadType == self::DOWNLOAD_TYPE_STREAM) {
$objWriter->save('php://output'); $objWriter->save('php://output');
} else { } else {
$f = $_SERVER['DOCUMENT_ROOT'] . "/tmp/data"; $f = $this->rootPath . "/export";
if (!file_exists($f)) { if (!file_exists($f)) {
mkdir($f, 0777, true); mkdir($f, 0777, true);
} }
...@@ -326,13 +332,11 @@ class Exporter ...@@ -326,13 +332,11 @@ class Exporter
} }
break; break;
case self::DOWNLOAD_TYPE_RETURN_FILE_PATH: case self::DOWNLOAD_TYPE_RETURN_FILE_PATH:
$filePath = BASE_PATH . '/public/upload/'; $filePath = $this->rootPath . "/export/";
!is_dir($filePath) && mkdir($filePath, 0777, true); !is_dir($filePath) && mkdir($filePath, 0777, true);
$salt = md5(date("YmdHis") . '-' . rand(1, 300)); $fileName = $filePath . date('YmdHis') . '-' . $filename;
$returnFilename = 'upload/' . $salt . '-' . $filename;
$fileName = $filePath . $salt . '-' . $filename;
$objWriter->save($fileName); $objWriter->save($fileName);
return config('app_domain') . '/' . $returnFilename; return $this->config->get('app_domain') . str_replace($this->rootPath, '', $fileName);
break; break;
default: default:
throw new \Exception('不支持此种下载类型'); throw new \Exception('不支持此种下载类型');
...@@ -351,10 +355,11 @@ class Exporter ...@@ -351,10 +355,11 @@ class Exporter
/** /**
* 返回原生的PhpOffice Reader 手动处理数据 * 返回原生的PhpOffice Reader 手动处理数据
* @return null|\PhpOffice\PhpSpreadsheet\Spreadsheet * @return null|Spreadsheet
*/ */
public function getReader() public function getReader()
{ {
return $this->reader; return $this->reader;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment