Commit f8374663 authored by 王源's avatar 王源 🎧

设置excel锁定密码,编写excel上传公共文件

parent 6c3ee77b
...@@ -58,14 +58,32 @@ class UploadManager ...@@ -58,14 +58,32 @@ class UploadManager
return self::uploadFile($excel, $options); return self::uploadFile($excel, $options);
} }
/**
* 表格上传方法获取真实地址
* @param $excel
* @param array $options
* @return string
* @throws HttpResponseException
*/
public static function uploadExcelGetRealPath($excel, $options = [])
{
$excelOptions = [
'path' => 'excel',
'mime' => ['xlsx', 'xls']
];
$options = array_merge($excelOptions, $options);
return self::uploadFile($excel, $options, true);
}
/** /**
* 文件上传方法 * 文件上传方法
* @param UploadedFile $file 上传的文件 * @param UploadedFile $file 上传的文件
* @param array $options 配置参数 * @param array $options 配置参数
* @param bool $realPath
* @return string * @return string
* @throws HttpResponseException * @throws HttpResponseException
*/ */
public static function uploadFile($file, $options = []) public static function uploadFile($file, $options = [], $realPath = false)
{ {
$documentRoot = config('server.settings.document_root'); $documentRoot = config('server.settings.document_root');
if (!$documentRoot) { if (!$documentRoot) {
...@@ -87,7 +105,11 @@ class UploadManager ...@@ -87,7 +105,11 @@ class UploadManager
$savePath = self::parsePath($options, $documentRoot) . $fileName; $savePath = self::parsePath($options, $documentRoot) . $fileName;
$file->moveTo($savePath); $file->moveTo($savePath);
if ($file->isMoved()) { if ($file->isMoved()) {
if ($realPath) {
return $savePath;
} else {
return str_replace($documentRoot, '', $savePath); return str_replace($documentRoot, '', $savePath);
}
} else { } else {
throw new HttpResponseException('文件保存失败'); throw new HttpResponseException('文件保存失败');
} }
......
<?php
/**
* Created by PhpStorm.
* User: zero
* Date: 2020/6/1
* Time: 14:06
* Description:
*/
namespace Meibuyu\Micro\Tools;
use Hyperf\Contract\ConfigInterface;
use Meibuyu\Micro\Exceptions\HttpResponseException;
use Meibuyu\Micro\Manager\UploadManager;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class ExcelImporter
{
/**
* @var ConfigInterface
*/
private $config;
/**
* @var Spreadsheet|null
*/
private $reader = null;
/**
* @var Worksheet|null
*/
private $sheet = null;
private $rootPath;
private $sheetIndex = 0;
private $filePath;
private $fileType;
private $highestRow = 0; // 总行数
private $highestColumn = 0; // 最后一列字符
private $errorColumn;
/**
* Importer constructor.
* @param $file
* @param int $sheetIndex
* @throws \Meibuyu\Micro\Exceptions\HttpResponseException
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
*/
public function __construct($file, $sheetIndex = 0)
{
$this->config = container(ConfigInterface::class);
$this->rootPath = $this->config->get('server.settings.document_root', BASE_PATH . '/public');
$path = UploadManager::uploadExcelGetRealPath($file);
$this->fileType = ucfirst(strtolower(pathinfo($file, PATHINFO_EXTENSION)));
$this->reader = IOFactory::createReaderForFile($path)->load($path);
$this->filePath = $path;
$this->sheet = $this->reader->getSheet($sheetIndex);
$this->sheetIndex = $sheetIndex;
$this->highestRow = $this->sheet->getHighestRow();
$this->highestColumn = $this->sheet->getHighestColumn();
$this->highestColumn++;
}
/**
* 判断excel表中数据不能为空
* @param $least
* @return bool
* @throws HttpResponseException
*/
public function checkEmpty($least)
{
if ($this->highestRow < $least) {
return false;
} else {
return true;
}
}
/**
* 获取表格字符串
* @param string $startCol 开始的列,默认为A
* @param null $endCol 结束的列,默认为表格已有数据的最后一列
* @return string
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
public function getHeaderStr($startCol = 'A', $endCol = null): string
{
$str = '';
$endCol = $endCol ? $endCol : $this->highestColumn;
for ($i = $startCol; $i != $endCol; $i++) {
$str .= $this->sheet->getCell($i . 1)->getFormattedValue();
}
return $str;
}
/**
* 验证表头
* @param string $headerStr 对比的表头字符串
* @param string $startCol 开始的列,默认为A
* @param null $endCol 结束的列,默认为表格已有数据的最后一列
* @return bool
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
public function checkHeaderStr(string $headerStr, $startCol = 'A', $endCol = null)
{
$excelField = $this->getHeaderStr($startCol, $endCol);
if (md5($excelField) == md5($headerStr)) {
return true;
} else {
return false;
}
}
/**
* 必填检测数据
* @param $requires
* @param $errorCount
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
public function checkRequire($requires, $errorCount)
{
foreach ($requires as $k => $v) {
if ($v === '') {
$this->setErrorMessage($k . "不能为空", $errorCount);
continue;
}
}
}
/**
* 必填检测数据
* @param $data
* @param $errorCount
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
public function checkNumeric($data, $errorCount)
{
foreach ($data as $k => $v) {
if (!is_numeric($v)) {
$this->setErrorMessage("{$k}只能是数字", $errorCount);
continue;
}
}
}
/**
* 设置错误标识
* @param $p
* @param $message
* @param $count
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
public function setErrorMessage($message, &$count)
{
$count++;
$this->sheet->getCell($this->errorColumn)->setValue($message);
$this->sheet->getStyle($this->errorColumn)->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
}
/**
* 保存
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function save()
{
$this->reader->setActiveSheetIndex(0);
IOFactory::createWriter($this->reader, $this->fileType)->save($this->filePath);
}
/**
* 返回当前激化的sheet表
* @return null|Worksheet
*/
public function getSheet()
{
return $this->sheet;
}
/**
* 返回原生的PhpOffice Reader 手动处理数据
* @return null|Spreadsheet
*/
public function getReader()
{
return $this->reader;
}
/**
* 获取总行数
* @return int
*/
public function getHighestRow()
{
return $this->highestRow;
}
/**
* 返回文件本地地址
* @return string
*/
public function getFilePath()
{
return $this->filePath;
}
/**
* 返回文件网络地址
* @return string
*/
public function getFileUrl()
{
return $this->config->get('app_domain') . str_replace($this->rootPath, '', $this->filePath);
}
public function setErrorColumn($column)
{
$this->errorColumn = $column;
}
public function getErrorColumn()
{
return $this->errorColumn;
}
}
...@@ -106,6 +106,7 @@ class Exporter ...@@ -106,6 +106,7 @@ class Exporter
public function setUnprotectRange($unprotectRange = "") public function setUnprotectRange($unprotectRange = "")
{ {
$this->sheet->getProtection()->setSheet(true); $this->sheet->getProtection()->setSheet(true);
$this->sheet->getProtection()->setPassword('micro-ttt');
if ($unprotectRange) { if ($unprotectRange) {
$this->sheet->getStyle($unprotectRange) $this->sheet->getStyle($unprotectRange)
->getProtection() ->getProtection()
......
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