在JSP页面中
< script type= "text/javascript" >
$ ( document) . ready ( function ( ) {
$ ( "#btnImport" ) . click ( function ( ) {
$. jBox ( $ ( "#importBox" ) . html ( ) , { title: "导入数据" , buttons: { "关闭" : true } ,
bottomText: "导入文件不能超过5M,仅允许导入“xls”或“xlsx”格式文件!" } ) ;
} ) ;
} ) ;
< / script>
在body中添加隐藏域
< body>
< div id= "importBox" class = "hide" >
< form id= "importForm" action= "${ctx}/basic/personInfo/import"
method= "post" enctype= "multipart/form-data" class = "form-search"
style= "padding-left: 20px; text-align: center;"
onsubmit= "loading('正在导入,请稍等...');" >
< br / >
< input id= "uploadFile" name= "file" type= "file"
style= "width: 330px" / >
< br / > < br / >
< input id= "btnImportSubmit"
class = "btn btn-primary" type= "submit" value= " 导 入 " / > < a
href= "${ctx}/basic/personInfo/import/template" > 下载模板< / a>
< / form>
< / div>
添加导入按钮
<li class="btns"><input id="btnImport" class="btn btn-primary" type="button" value="导入"/></li>
实体类中
* 人员信息Entity
*
* @author zhaojf
* @version 2018 - 11 - 23
* /
public class PersonInfo extends DataEntity < PersonInfo> {
private static final long serialVersionUID = 1 L;
private String name;
private RushDisposalStation station;
private PersonType type;
private String phone;
private String nativePlace;
private String card;
private String address;
public PersonInfo ( ) {
super ( ) ;
}
public PersonInfo ( String id) {
super ( id) ;
}
@Length ( min = 0 , max = 64 , message = "姓名长度必须介于 0 和 64 之间" )
@ExcelField ( title = "姓名" , align= 2 , sort= 10 )
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
@ExcelField ( title = "归属垃圾站" , align= 2 , sort= 15 )
public RushDisposalStation getStation ( ) {
return station;
}
public void setStation ( RushDisposalStation station) {
this . station = station;
}
@ExcelField ( title = "人员类别" , align= 2 , sort= 20 )
public PersonType getType ( ) {
return type;
}
public void setType ( PersonType type) {
this . type = type;
}
@Length ( min = 0 , max = 64 , message = "电话长度必须介于 0 和 64 之间" )
@ExcelField ( title = "电话" , align= 2 , sort= 35 )
public String getPhone ( ) {
return phone;
}
public void setPhone ( String phone) {
this . phone = phone;
}
@Length ( min = 0 , max = 64 , message = "贯籍长度必须介于 0 和 64 之间" )
@ExcelField ( title = "贯籍" , align= 2 , sort= 25 )
public String getNativePlace ( ) {
return nativePlace;
}
public void setNativePlace ( String nativePlace) {
this . nativePlace = nativePlace;
}
@ExcelField ( title = "身份证号码" , align= 2 , sort= 30 )
public String getCard ( ) {
return card;
}
public void setCard ( String card) {
this . card = card;
}
@ExcelField ( title = "家庭住址" , align= 2 , sort= 40 )
public String getAddress ( ) {
return address;
}
public void setAddress ( String address) {
this . address = address;
}
}
在Controller层
@RequiresPermissions ( "basic:personInfo:view" )
@RequestMapping ( value = "import/template" )
public String importFileTemplate ( HttpServletResponse response, RedirectAttributes redirectAttributes) {
try {
String fileName = "人员信息导入模板.xlsx" ;
List< PersonInfo> list = Lists. newArrayList ( ) ;
list. add ( new PersonInfo ( ) ) ;
new ExportExcel ( "人员信息" , PersonInfo. class , 2 ) . setDataList ( list) . write ( response, fileName) . dispose ( ) ;
return null;
} catch ( Exception e) {
addMessage ( redirectAttributes, "导入模板下载失败!失败信息:" + e. getMessage ( ) ) ;
}
return "redirect:" + Global. getAdminPath ( ) + "/basic/personInfo/?repage" ;
}
@RequiresPermissions ( "basic:personInfo:edit" )
@RequestMapping ( value = "import" )
public String importFile ( MultipartFile file, RedirectAttributes redirectAttributes) {
try {
String msg = personInfoService. saveExcel ( file) ;
addMessage ( redirectAttributes, msg) ;
} catch ( Exception e) {
addMessage ( redirectAttributes, "导入人员信息失败!失败信息:" + e. getMessage ( ) ) ;
}
return "redirect:" + Global. getAdminPath ( ) + "/basic/personInfo/?repage" ;
}
Service层中(根据需求不同,进行改动)
@Transactional ( readOnly = false )
public String saveExcel ( MultipartFile file) throws Exception{
int successNum = 0 ;
ImportExcel ei = new ImportExcel ( file, 1 , 0 ) ;
Map< String, List> map = myGetCell ( ei) ;
List< RushDisposalStation> rushDisposalStationList = map. get ( "rushDisposalStationList" ) ;
List< PersonType> personTypeList = map. get ( "personTypeList" ) ;
List< PersonInfo> list = ei. getDataList ( PersonInfo. class ) ;
int i = 0 ;
DecimalFormat decimalFormat = new DecimalFormat ( "0" ) ;
for ( PersonInfo personinfo : list
) {
personinfo. setStation ( rushDisposalStationList. get ( i) ) ;
personinfo. setType ( personTypeList. get ( i++ ) ) ;
double parseDouble = Double. parseDouble ( personinfo. getCard ( ) ) ;
String card = decimalFormat. format ( parseDouble) ;
personinfo. setCard ( card) ;
String phone = decimalFormat. format ( Double. parseDouble ( personinfo. getPhone ( ) ) ) ;
personinfo. setPhone ( phone) ;
super . save ( personinfo) ;
successNum++ ;
}
return "已成功导入 " + successNum + " 条参赛者信息记录" ;
}
public Map< String, List> myGetCell ( ImportExcel ei) {
HashMap< String, List> map = new HashMap < > ( 16 ) ;
List< String> stationNamelist = Lists. newArrayList ( ) ;
List< String> typeNamelist = Lists. newArrayList ( ) ;
for ( int i = ei. getDataRowNum ( ) ; i < ei. getLastDataRowNum ( ) ; i++ ) {
Row row = ei. getRow ( i) ;
if ( ei. getCellValue ( row, 1 ) != null) {
stationNamelist. add ( ei. getCellValue ( row, 1 ) . toString ( ) ) ;
}
if ( ei. getCellValue ( row, 2 ) != null) {
typeNamelist. add ( ei. getCellValue ( row, 2 ) . toString ( ) ) ;
}
}
List< RushDisposalStation> rushDisposalStationList = null;
if ( stationNamelist != null && ! stationNamelist. isEmpty ( ) ) {
rushDisposalStationList = rushDisposalStationDao. findByStationNameList ( stationNamelist) ;
}
List< PersonType> personTypeList = null;
if ( typeNamelist != null && ! typeNamelist. isEmpty ( ) ) {
personTypeList = personTypeDao. findByTypeNameList ( typeNamelist) ;
}
map. put ( "rushDisposalStationList" , rushDisposalStationList) ;
map. put ( "personTypeList" , personTypeList) ;
return map;
}
关联的实体类RushDisposalStation的dao
List< RushDisposalStation> findByStationNameList ( @Param ( "stationNameList" ) List< String> stationNameList) ;
关联的实体类RushDisposalStation的dao.xml
< select id = " findByStationNameList" resultType = " RushDisposalStation" >
SELECT
< include refid = " rushDisposalStationColumns" />
FROM
rush_disposal_station a
< include refid = " rushDisposalStationJoins" />
WHERE
a.del_flag = '0'
AND a.name IN
< foreach collection = " stationNameList" item = " item" open = " (" separator = " ," close = " )" >
#{item}
</ foreach>
</ select>