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;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xls;
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
||||
|
||||
trait Backend
|
||||
{
|
||||
|
||||
|
|
@ -294,27 +299,43 @@ trait Backend
|
|||
protected function import()
|
||||
{
|
||||
$file = $this->request->request('file');
|
||||
if (!$file)
|
||||
{
|
||||
if (!$file) {
|
||||
$this->error(__('Parameter %s can not be empty', 'file'));
|
||||
}
|
||||
$filePath = ROOT_PATH . DS . 'public' . DS . $file;
|
||||
if (!is_file($filePath))
|
||||
{
|
||||
if (!is_file($filePath)) {
|
||||
$this->error(__('No results were found'));
|
||||
}
|
||||
$PHPReader = new \PHPExcel_Reader_Excel2007();
|
||||
if (!$PHPReader->canRead($filePath))
|
||||
{
|
||||
$PHPReader = new \PHPExcel_Reader_Excel5();
|
||||
if (!$PHPReader->canRead($filePath))
|
||||
{
|
||||
$PHPReader = new \PHPExcel_Reader_CSV();
|
||||
if (!$PHPReader->canRead($filePath))
|
||||
{
|
||||
$this->error(__('Unknown data format'));
|
||||
//实例化reader
|
||||
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
||||
if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
|
||||
$this->error(__('Unknown data format'));
|
||||
}
|
||||
if ($ext === 'csv') {
|
||||
$file = fopen($filePath, 'r');
|
||||
$filePath = tempnam(sys_get_temp_dir(), 'import_csv');
|
||||
$fp = fopen($filePath, "w");
|
||||
$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
|
||||
|
|
@ -324,64 +345,58 @@ trait Backend
|
|||
$database = \think\Config::get('database.database');
|
||||
$fieldArr = [];
|
||||
$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)
|
||||
{
|
||||
if ($importHeadType == 'comment')
|
||||
{
|
||||
foreach ($list as $k => $v) {
|
||||
if ($importHeadType == 'comment') {
|
||||
$fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
|
||||
}
|
||||
}
|
||||
|
||||
$PHPExcel = $PHPReader->load($filePath); //加载文件
|
||||
$currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
|
||||
$allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
|
||||
$allRow = $currentSheet->getHighestRow(); //取得一共有多少行
|
||||
$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;
|
||||
//加载文件
|
||||
try {
|
||||
if (!$PHPExcel = $reader->load($filePath)) {
|
||||
$this->error(__('Unknown data format'));
|
||||
}
|
||||
}
|
||||
$insert = [];
|
||||
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;
|
||||
$currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
|
||||
$allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
|
||||
$allRow = $currentSheet->getHighestRow(); //取得一共有多少行
|
||||
$maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
|
||||
for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
|
||||
for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
|
||||
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
||||
$fields[] = $val;
|
||||
}
|
||||
}
|
||||
if ($row)
|
||||
{
|
||||
$insert[] = $row;
|
||||
|
||||
$insert = [];
|
||||
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'));
|
||||
}
|
||||
try
|
||||
{
|
||||
|
||||
try {
|
||||
$this->model->saveAll($insert);
|
||||
}
|
||||
catch (\think\exception\PDOException $exception)
|
||||
{
|
||||
} catch (\think\exception\PDOException $exception) {
|
||||
$this->error($exception->getMessage());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,7 +24,8 @@
|
|||
"phpmailer/phpmailer": "^5.2",
|
||||
"karsonzhang/fastadmin-addons": "~1.1.0",
|
||||
"overtrue/pinyin": "~3.0",
|
||||
"phpoffice/phpexcel": "^1.8"
|
||||
"phpoffice/phpspreadsheet": "^1.2",
|
||||
"phpoffice/phpword": "^0.14.0"
|
||||
},
|
||||
"config": {
|
||||
"preferred-install": "dist"
|
||||
|
|
|
|||
Loading…
Reference in New Issue