mirror of https://gitee.com/karson/fastadmin.git
升级为phpoffice/phpspreadsheet并优化CSV导入功能
composer require phpoffice/phpspreadsheet composer remove phpoffice/phpexcel composer update composer require phpoffice/phpwordpull/71/head
parent
4457fcfbb5
commit
4365537cd5
|
|
@ -2,6 +2,11 @@
|
||||||
|
|
||||||
namespace app\admin\library\traits;
|
namespace app\admin\library\traits;
|
||||||
|
|
||||||
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Reader\Xls;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
||||||
|
|
||||||
trait Backend
|
trait Backend
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -294,27 +299,43 @@ trait Backend
|
||||||
protected function import()
|
protected function import()
|
||||||
{
|
{
|
||||||
$file = $this->request->request('file');
|
$file = $this->request->request('file');
|
||||||
if (!$file)
|
if (!$file) {
|
||||||
{
|
|
||||||
$this->error(__('Parameter %s can not be empty', 'file'));
|
$this->error(__('Parameter %s can not be empty', 'file'));
|
||||||
}
|
}
|
||||||
$filePath = ROOT_PATH . DS . 'public' . DS . $file;
|
$filePath = ROOT_PATH . DS . 'public' . DS . $file;
|
||||||
if (!is_file($filePath))
|
if (!is_file($filePath)) {
|
||||||
{
|
|
||||||
$this->error(__('No results were found'));
|
$this->error(__('No results were found'));
|
||||||
}
|
}
|
||||||
$PHPReader = new \PHPExcel_Reader_Excel2007();
|
//实例化reader
|
||||||
if (!$PHPReader->canRead($filePath))
|
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
||||||
{
|
if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
|
||||||
$PHPReader = new \PHPExcel_Reader_Excel5();
|
$this->error(__('Unknown data format'));
|
||||||
if (!$PHPReader->canRead($filePath))
|
}
|
||||||
{
|
if ($ext === 'csv') {
|
||||||
$PHPReader = new \PHPExcel_Reader_CSV();
|
$file = fopen($filePath, 'r');
|
||||||
if (!$PHPReader->canRead($filePath))
|
$filePath = tempnam(sys_get_temp_dir(), 'import_csv');
|
||||||
{
|
$fp = fopen($filePath, "w");
|
||||||
$this->error(__('Unknown data format'));
|
$n = 0;
|
||||||
|
while ($line = fgets($file)) {
|
||||||
|
$line = rtrim($line, "\n\r\0");
|
||||||
|
$encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
|
||||||
|
if ($encoding != 'utf-8') {
|
||||||
|
$line = mb_convert_encoding($line, 'utf-8', $encoding);
|
||||||
}
|
}
|
||||||
|
if ($n == 0 || preg_match('/^".*"$/', $line)) {
|
||||||
|
fwrite($fp, $line . "\n");
|
||||||
|
} else {
|
||||||
|
fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
|
||||||
|
}
|
||||||
|
$n++;
|
||||||
}
|
}
|
||||||
|
fclose($file) || fclose($fp);
|
||||||
|
|
||||||
|
$reader = new Csv();
|
||||||
|
} elseif ($ext === 'xls') {
|
||||||
|
$reader = new Xls();
|
||||||
|
} else {
|
||||||
|
$reader = new Xlsx();
|
||||||
}
|
}
|
||||||
|
|
||||||
//导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
|
//导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
|
||||||
|
|
@ -324,64 +345,58 @@ trait Backend
|
||||||
$database = \think\Config::get('database.database');
|
$database = \think\Config::get('database.database');
|
||||||
$fieldArr = [];
|
$fieldArr = [];
|
||||||
$list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
|
$list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
|
||||||
foreach ($list as $k => $v)
|
foreach ($list as $k => $v) {
|
||||||
{
|
if ($importHeadType == 'comment') {
|
||||||
if ($importHeadType == 'comment')
|
|
||||||
{
|
|
||||||
$fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
|
$fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
$fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
|
$fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$PHPExcel = $PHPReader->load($filePath); //加载文件
|
//加载文件
|
||||||
$currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
|
try {
|
||||||
$allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
|
if (!$PHPExcel = $reader->load($filePath)) {
|
||||||
$allRow = $currentSheet->getHighestRow(); //取得一共有多少行
|
$this->error(__('Unknown data format'));
|
||||||
$maxColumnNumber = \PHPExcel_Cell::columnIndexFromString($allColumn);
|
|
||||||
for ($currentRow = 1; $currentRow <= 1; $currentRow++)
|
|
||||||
{
|
|
||||||
for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++)
|
|
||||||
{
|
|
||||||
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
||||||
$fields[] = $val;
|
|
||||||
}
|
}
|
||||||
}
|
$currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
|
||||||
$insert = [];
|
$allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
|
||||||
for ($currentRow = 2; $currentRow <= $allRow; $currentRow++)
|
$allRow = $currentSheet->getHighestRow(); //取得一共有多少行
|
||||||
{
|
$maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
|
||||||
$values = [];
|
for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
|
||||||
for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++)
|
for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
|
||||||
{
|
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
||||||
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
$fields[] = $val;
|
||||||
$values[] = is_null($val) ? '' : $val;
|
|
||||||
}
|
|
||||||
$row = [];
|
|
||||||
$temp = array_combine($fields, $values);
|
|
||||||
foreach ($temp as $k => $v)
|
|
||||||
{
|
|
||||||
if (isset($fieldArr[$k]) && $k !== '')
|
|
||||||
{
|
|
||||||
$row[$fieldArr[$k]] = $v;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($row)
|
|
||||||
{
|
$insert = [];
|
||||||
$insert[] = $row;
|
for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
|
||||||
|
$values = [];
|
||||||
|
for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
|
||||||
|
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
||||||
|
$values[] = is_null($val) ? '' : $val;
|
||||||
|
}
|
||||||
|
$row = [];
|
||||||
|
$temp = array_combine($fields, $values);
|
||||||
|
foreach ($temp as $k => $v) {
|
||||||
|
if (isset($fieldArr[$k]) && $k !== '') {
|
||||||
|
$row[$fieldArr[$k]] = $v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($row) {
|
||||||
|
$insert[] = $row;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (Exception $exception) {
|
||||||
|
$this->error($exception->getMessage());
|
||||||
}
|
}
|
||||||
if (!$insert)
|
if (!$insert) {
|
||||||
{
|
|
||||||
$this->error(__('No rows were updated'));
|
$this->error(__('No rows were updated'));
|
||||||
}
|
}
|
||||||
try
|
|
||||||
{
|
try {
|
||||||
$this->model->saveAll($insert);
|
$this->model->saveAll($insert);
|
||||||
}
|
} catch (\think\exception\PDOException $exception) {
|
||||||
catch (\think\exception\PDOException $exception)
|
|
||||||
{
|
|
||||||
$this->error($exception->getMessage());
|
$this->error($exception->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,8 @@
|
||||||
"phpmailer/phpmailer": "^5.2",
|
"phpmailer/phpmailer": "^5.2",
|
||||||
"karsonzhang/fastadmin-addons": "~1.1.0",
|
"karsonzhang/fastadmin-addons": "~1.1.0",
|
||||||
"overtrue/pinyin": "~3.0",
|
"overtrue/pinyin": "~3.0",
|
||||||
"phpoffice/phpexcel": "^1.8"
|
"phpoffice/phpspreadsheet": "^1.2",
|
||||||
|
"phpoffice/phpword": "^0.14.0"
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"preferred-install": "dist"
|
"preferred-install": "dist"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue