Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Springboot Plus
Commits
c85119ce
Commit
c85119ce
authored
Mar 05, 2018
by
李家智
Browse files
upload excel
parent
3b59d4e7
Changes
4
Hide whitespace changes
Inline
Side-by-side
admin-console/src/main/java/com/ibeetl/admin/console/service/DictConsoleService.java
View file @
c85119ce
...
...
@@ -5,6 +5,7 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.Map
;
import
org.apache.poi.ss.util.CellReference
;
import
org.beetl.sql.core.engine.PageQuery
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
...
...
@@ -55,7 +56,7 @@ public class DictConsoleService extends BaseService<CoreDict>{
* @return
*/
public
void
batchInsert
(
List
<
DictExcelImportData
>
list
)
{
int
dataStartRow
=
3
;
int
dataStartRow
=
2
;
final
Map
<
Integer
,
DictExcelImportData
>
map
=
new
HashMap
<>();
list
.
forEach
((
item
)->
map
.
put
(
item
.
getExcelId
(),
item
));
//逐个按照顺序导入
...
...
@@ -66,33 +67,35 @@ public class DictConsoleService extends BaseService<CoreDict>{
dict
.
setType
(
item
.
getType
());
dict
.
setTypeName
(
item
.
getTypeName
());
dict
.
setValue
(
item
.
getValue
());
//导入前先查找是否有此值
CoreDict
template
=
new
CoreDict
();
template
.
setType
(
dict
.
getType
());
template
.
setValue
(
dict
.
getValue
());
CoreDict
dbDict
=
dictDao
.
templateOne
(
template
);
if
(
dbDict
!=
null
)
{
int
row
=
item
.
getExcelId
()+
dataStartRow
;
throwImporError
(
row
,
0
,
"字典数据已经存在"
);
}
//设置父字典
if
(
item
.
getParentExcelId
()!=
0
)
{
DictExcelImportData
parentItem
=
map
.
get
(
item
.
getParentExcelId
());
if
(
parentItem
==
null
)
{
//硬编码,TODO,用reader缺少校验,替换手写导入
int
row
=
item
.
getExcelId
()+
dataStartRow
;
throwImporError
(
row
,
6
,
"未找到父字典"
);
throwImporError
(
row
,
5
,
"未找到父字典"
);
}
if
(
parentItem
.
getId
()==
null
)
{
int
row
=
item
.
getExcelId
()+
dataStartRow
;
throwImporError
(
row
,
6
,
"父字典未被导入,请先导入父字典"
);
throwImporError
(
row
,
5
,
"父字典未被导入,请先导入父字典"
);
}
dict
.
setParent
(
parentItem
.
getId
());
}
dict
.
setCreateTime
(
new
Date
());
//导入前先查找是否有此值
CoreDict
template
=
new
CoreDict
();
template
.
setType
(
dict
.
getType
());
template
.
setValue
(
dict
.
getValue
());
CoreDict
dbDict
=
dictDao
.
templateOne
(
template
);
if
(
dbDict
!=
null
)
{
int
row
=
item
.
getExcelId
()+
dataStartRow
;
throwImporError
(
row
,
0
,
"字典数据已经存在"
);
}
dictDao
.
insert
(
dict
);
item
.
setId
(
dict
.
getId
());
dataStartRow
++;
}
...
...
@@ -100,10 +103,10 @@ public class DictConsoleService extends BaseService<CoreDict>{
private
void
throwImporError
(
int
row
,
int
col
,
String
msg
)
{
ExcelError
error
=
new
ExcelError
();
error
.
setRow
(
row
);
error
.
setC
o
l
(
c
ol
);
CellReference
cr
=
new
CellReference
(
row
,
col
,
false
,
false
);
error
.
setC
el
l
(
c
r
.
formatAsString
()
);
error
.
setMsg
(
msg
);
throw
new
PlatformException
(
error
.
toString
()
);
throw
new
PlatformException
(
"导入错误在:"
+
error
.
getCell
()+
","
+
msg
);
}
...
...
admin-console/src/main/java/com/ibeetl/admin/console/web/DictConsoleController.java
View file @
c85119ce
...
...
@@ -16,6 +16,8 @@ import org.apache.commons.logging.LogFactory;
import
org.beetl.sql.core.engine.PageQuery
;
import
org.jxls.common.Context
;
import
org.jxls.reader.ReaderBuilder
;
import
org.jxls.reader.ReaderConfig
;
import
org.jxls.reader.XLSReadMessage
;
import
org.jxls.reader.XLSReadStatus
;
import
org.jxls.reader.XLSReader
;
import
org.jxls.util.JxlsHelper
;
...
...
@@ -171,18 +173,27 @@ public class DictConsoleController{
if
(
file
.
isEmpty
())
{
return
JsonResult
.
fail
();
}
String
fileName
=
file
.
getOriginalFilename
();
InputStream
ins
=
file
.
getInputStream
();
InputStream
inputXML
=
Thread
.
currentThread
().
getContextClassLoader
().
getResourceAsStream
(
"excelTemplates/admin/dict/dict_mapping.xml"
);
XLSReader
mainReader
=
ReaderBuilder
.
buildFromXML
(
inputXML
);
InputStream
inputXLS
=
ins
;
List
<
DictExcelImportData
>
dicts
=
new
ArrayList
<
DictExcelImportData
>();
Map
beans
=
new
HashMap
();
beans
.
put
(
"list"
,
dicts
);
ReaderConfig
.
getInstance
().
setSkipErrors
(
true
);
XLSReadStatus
readStatus
=
mainReader
.
read
(
inputXLS
,
beans
);
// this.dictService.batchInsert(dicts);//TODO,为啥这里抛的错误,layui对话框不能正确处理http 500错误,改成下面方式
List
<
XLSReadMessage
>
errors
=
readStatus
.
getReadMessages
();
if
(!
errors
.
isEmpty
())
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
XLSReadMessage
msg:
errors
)
{
sb
.
append
(
parseXLSReadMessage
(
msg
));
sb
.
append
(
","
);
}
sb
.
setLength
(
sb
.
length
()-
1
);
return
JsonResult
.
failMessage
(
"解析excel出错:"
+
sb
.
toString
());
}
// this.dictService.batchInsert(dicts);//layui对话框不能正确处理http 500错误,改成下面方式
// return JsonResult.success();
try
{
this
.
dictService
.
batchInsert
(
dicts
);
...
...
@@ -192,5 +203,14 @@ public class DictConsoleController{
}
}
/*xlsReader 设计有问题,还需要通过解析msg知道错误位置在哪里*/
private
String
parseXLSReadMessage
(
XLSReadMessage
msg
)
{
// String message = "Can't read cell " + getCellName(mapping, rowShift) + " on " + cursor.getSheetName() + " spreadsheet";
String
str
=
msg
.
getMessage
();
int
start
=
"Can't read cell "
.
length
();
int
end
=
str
.
indexOf
(
"on"
);
return
str
.
substring
(
start
,
end
);
}
}
admin-console/src/main/resources/excelTemplates/admin/dict/dict_upload_template.xls
View file @
c85119ce
No preview for this file type
admin-core/src/main/java/com/ibeetl/admin/core/util/ExcelError.java
View file @
c85119ce
package
com.ibeetl.admin.core.util
;
public
class
ExcelError
{
Integer
row
;
Integer
col
;
String
cel
;
String
cell
;
String
msg
;
public
Integer
getRow
()
{
return
row
;
public
String
getCell
()
{
return
cell
;
}
public
void
setRow
(
Integer
row
)
{
this
.
row
=
row
;
}
public
Integer
getCol
()
{
return
col
;
}
public
void
setCol
(
Integer
col
)
{
this
.
col
=
col
;
}
public
String
getCel
()
{
return
cel
;
}
public
void
setCel
(
String
cel
)
{
this
.
cel
=
cel
;
public
void
setCell
(
String
cell
)
{
this
.
cell
=
cell
;
}
public
String
getMsg
()
{
return
msg
;
...
...
@@ -29,10 +18,7 @@ public class ExcelError {
public
void
setMsg
(
String
msg
)
{
this
.
msg
=
msg
;
}
@Override
public
String
toString
()
{
return
"ExcelError [row="
+
row
+
", col="
+
col
+
", cel="
+
cel
+
", msg="
+
msg
+
"]"
;
}
}
Write
Preview
Supports
Markdown
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