Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-micro
Commits
f8374663
Commit
f8374663
authored
Jun 01, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
设置excel锁定密码,编写excel上传公共文件
parent
6c3ee77b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
256 additions
and
2 deletions
+256
-2
UploadManager.php
src/Manager/UploadManager.php
+24
-2
ExcelImporter.php
src/Tools/ExcelImporter.php
+231
-0
Exporter.php
src/Tools/Exporter.php
+1
-0
No files found.
src/Manager/UploadManager.php
View file @
f8374663
...
...
@@ -58,14 +58,32 @@ class UploadManager
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 array $options 配置参数
* @param bool $realPath
* @return string
* @throws HttpResponseException
*/
public
static
function
uploadFile
(
$file
,
$options
=
[])
public
static
function
uploadFile
(
$file
,
$options
=
[]
,
$realPath
=
false
)
{
$documentRoot
=
config
(
'server.settings.document_root'
);
if
(
!
$documentRoot
)
{
...
...
@@ -87,7 +105,11 @@ class UploadManager
$savePath
=
self
::
parsePath
(
$options
,
$documentRoot
)
.
$fileName
;
$file
->
moveTo
(
$savePath
);
if
(
$file
->
isMoved
())
{
return
str_replace
(
$documentRoot
,
''
,
$savePath
);
if
(
$realPath
)
{
return
$savePath
;
}
else
{
return
str_replace
(
$documentRoot
,
''
,
$savePath
);
}
}
else
{
throw
new
HttpResponseException
(
'文件保存失败'
);
}
...
...
src/Tools/ExcelImporter.php
0 → 100644
View file @
f8374663
<?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
;
}
}
src/Tools/Exporter.php
View file @
f8374663
...
...
@@ -106,6 +106,7 @@ class Exporter
public
function
setUnprotectRange
(
$unprotectRange
=
""
)
{
$this
->
sheet
->
getProtection
()
->
setSheet
(
true
);
$this
->
sheet
->
getProtection
()
->
setPassword
(
'micro-ttt'
);
if
(
$unprotectRange
)
{
$this
->
sheet
->
getStyle
(
$unprotectRange
)
->
getProtection
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment