修复导入时在表字段超过26时的错误

新增导入Excel文件首行类型配置,默认注释或表字段名
修复表格在字段为null值时的报错
pull/29/head
Karson 2017-12-25 12:21:40 +08:00
parent 5a87939e86
commit 483b901cc4
4 changed files with 40 additions and 18 deletions

View File

@ -329,25 +329,35 @@ trait Backend
} }
} }
//导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
$importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
$table = $this->model->getQuery()->getTable(); $table = $this->model->getQuery()->getTable();
$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)
{ {
$fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME']; if ($importHeadType == 'comment')
{
$fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
}
else
{
$fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
}
} }
$PHPExcel = $PHPReader->load($filePath); //加载文件 $PHPExcel = $PHPReader->load($filePath); //加载文件
$currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表 $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
$allColumn = $currentSheet->getHighestColumn(); //取得最大的列号 $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
$allRow = $currentSheet->getHighestRow(); //取得一共有多少行 $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
$maxColumnNumber = \PHPExcel_Cell::columnIndexFromString($allColumn);
for ($currentRow = 1; $currentRow <= 1; $currentRow++) for ($currentRow = 1; $currentRow <= 1; $currentRow++)
{ {
for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++)
{ {
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue(); $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
$fields[] = $val; $fields[] = $val;
} }
} }
@ -355,11 +365,10 @@ trait Backend
for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) for ($currentRow = 2; $currentRow <= $allRow; $currentRow++)
{ {
$values = []; $values = [];
for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++)
{ {
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue(); /* * ord()将字符转为十进制数 */ $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
$values[] = is_null($val) ? '' : $val; $values[] = is_null($val) ? '' : $val;
//echo iconv('utf-8','gb2312', $val)."\t";
} }
$row = []; $row = [];
$temp = array_combine($fields, $values); $temp = array_combine($fields, $values);

View File

@ -77,6 +77,13 @@ class Backend extends Controller
*/ */
protected $multiFields = 'status'; protected $multiFields = 'status';
/**
* 导入文件首行类型
* 支持comment/name
* 表示注释或字段名
*/
protected $importHeadType = 'comment';
/** /**
* 引入后台控制器的traits * 引入后台控制器的traits
*/ */

View File

@ -9942,9 +9942,9 @@ define('table',['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstr
return '<img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" />'; return '<img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" />';
}, },
images: function (value, row, index) { images: function (value, row, index) {
value = value.toString(); value = value === null ? '' : value.toString();
var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center'; var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
var arr = value.toString().split(','); var arr = value.split(',');
var html = []; var html = [];
$.each(arr, function (i, value) { $.each(arr, function (i, value) {
value = value ? value : '/assets/img/blank.gif'; value = value ? value : '/assets/img/blank.gif';
@ -9959,7 +9959,7 @@ define('table',['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstr
if (typeof this.custom !== 'undefined') { if (typeof this.custom !== 'undefined') {
colorArr = $.extend(colorArr, this.custom); colorArr = $.extend(colorArr, this.custom);
} }
value = value.toString(); value = value === null ? '' : value.toString();
var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary'; var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
value = value.charAt(0).toUpperCase() + value.slice(1); value = value.charAt(0).toUpperCase() + value.slice(1);
//渲染状态 //渲染状态
@ -9983,6 +9983,7 @@ define('table',['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstr
return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>'; return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
}, },
flag: function (value, row, index) { flag: function (value, row, index) {
value = value === null ? '' : value.toString();
var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'}; var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
//如果字段列有定义custom //如果字段列有定义custom
if (typeof this.custom !== 'undefined') { if (typeof this.custom !== 'undefined') {
@ -9993,9 +9994,9 @@ define('table',['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstr
} }
//渲染Flag //渲染Flag
var html = []; var html = [];
var arr = value.toString().split(','); var arr = value.split(',');
$.each(arr, function (i, value) { $.each(arr, function (i, value) {
value = value.toString(); value = value === null ? '' : value.toString();
if (value == '') if (value == '')
return true; return true;
var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary'; var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
@ -14394,6 +14395,10 @@ define("addtabs", function(){});
if ($(elem.combo_input).attr('placeholder')) if ($(elem.combo_input).attr('placeholder'))
$(elem.combo_input).attr('placeholder_bak', $(elem.combo_input).attr('placeholder')); $(elem.combo_input).attr('placeholder_bak', $(elem.combo_input).attr('placeholder'));
} }
if ($(elem.container).parent().hasClass("input-group")) {
$(elem.container).height($(elem.container).parent().height());
}
this.elem = elem; this.elem = elem;
}; };

View File

@ -341,9 +341,9 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
return '<img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" />'; return '<img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" />';
}, },
images: function (value, row, index) { images: function (value, row, index) {
value = value.toString(); value = value === null ? '' : value.toString();
var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center'; var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
var arr = value.toString().split(','); var arr = value.split(',');
var html = []; var html = [];
$.each(arr, function (i, value) { $.each(arr, function (i, value) {
value = value ? value : '/assets/img/blank.gif'; value = value ? value : '/assets/img/blank.gif';
@ -358,7 +358,7 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
if (typeof this.custom !== 'undefined') { if (typeof this.custom !== 'undefined') {
colorArr = $.extend(colorArr, this.custom); colorArr = $.extend(colorArr, this.custom);
} }
value = value.toString(); value = value === null ? '' : value.toString();
var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary'; var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
value = value.charAt(0).toUpperCase() + value.slice(1); value = value.charAt(0).toUpperCase() + value.slice(1);
//渲染状态 //渲染状态
@ -382,6 +382,7 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>'; return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
}, },
flag: function (value, row, index) { flag: function (value, row, index) {
value = value === null ? '' : value.toString();
var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'}; var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
//如果字段列有定义custom //如果字段列有定义custom
if (typeof this.custom !== 'undefined') { if (typeof this.custom !== 'undefined') {
@ -392,9 +393,9 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
} }
//渲染Flag //渲染Flag
var html = []; var html = [];
var arr = value.toString().split(','); var arr = value.split(',');
$.each(arr, function (i, value) { $.each(arr, function (i, value) {
value = value.toString(); value = value === null ? '' : value.toString();
if (value == '') if (value == '')
return true; return true;
var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary'; var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';