最近在做bootstrap的项目,遇到过一些问题,但是在网上找的东西总不是很全面,但是项目还是要做的,所有就自己一边网上找,一边自己琢磨,现在整理了一下 希望你们能看懂。谢谢
1.先导入js哦,定义一个button
<script type
="application/javascript" src
="/lib/js/jquery-3.1.1.js"></script
>
<script type
="application/javascript" src
="/lib/js/fileinput.js"></script
>
<script type
="text/javascript"
src
="/lib/js/zh.js"></script
>
<div class="alarm">
<button class="btn btn-primary" id="upload">批量导入</button>
</div>
2.创建批量导入模态框
<!--批量导入操作-->
<!-- 模态框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">文件上传</h4>
</div>
<div class="modal-body">
<input id="f_upload" type="file" name="file"/>
</div>
</div>
</div>
</div>
3.js代码
<script
>
$(function(){
initUpload();
$("#upload").on("click",function(){
$("#myModal").modal("show");
});
})
function initUpload(){
$("#f_upload").fileinput({
language
: 'zh',
uploadUrl
: "/eaxmManager/eaxm/importExam",
allowedFileExtensions
: ["xls", "xlsx"],
dropZoneTitle
: '可以将文件拖放到这里',
uploadAsync
: true,
showPreview
: true,
showUpload
: true,
showRemove
: true,
showCancel
:true,
showCaption
: true,
browseClass
: "btn btn-primary",
dropZoneEnabled
: true,
maxFileSize
: 0,
minFileCount
: 1,
maxFileCount
: 1,
previewFileIcon
: "<i class='glyphicon glyphicon-king'></i>",
previewFileIconSettings
: {
'docx': '<i ass="fa fa-file-word-o text-primary"></i>',
'xlsx': '<i class="fa fa-file-excel-o text-success"></i>',
'xls': '<i class="fa fa-file-excel-o text-success"></i>',
'pptx': '<i class="fa fa-file-powerpoint-o text-danger"></i>',
'jpg': '<i class="fa fa-file-photo-o text-warning"></i>',
'pdf': '<i class="fa fa-file-archive-o text-muted"></i>',
'zip': '<i class="fa fa-file-archive-o text-muted"></i>',
},
msgFilesTooMany
: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
elErrorContainer
: '#kartik-file-errors'
}).on("fileuploaded", function(event
, data
, previewId
, index
) {
$(".modal").modal("hide");
$("#examTable").bootstrapTable('refresh');
console
.log("fileuploaded");
console
.log("event"+event
);
console
.log("data"+data
);
console
.log("previewId"+previewId
);
console
.log("index"+index
);
}).on('fileerror', function(event
, data
, msg
) {
console
.log("fileerror");
console
.log("event"+event
);
console
.log("data"+data
);
console
.log("msg"+msg
);
});
}
</script
>
4.点击批量导入按钮,显示模态框如下图,点击选择导入excel,点击上传即可
5.serviceImpl层,相关的注释写的很清楚注意看下面(注意下面是在第几个单元格开始读取)
@Override
public int insertExam(MultipartFile file
) throws Exception
{
int result
= 0;
List
<EaxmDTO> eaxmList
= new ArrayList<>();
String fileName
= file
.getOriginalFilename();
String suffix
= fileName
.substring(fileName
.lastIndexOf(".")+1);
InputStream ins
= file
.getInputStream();
Workbook wb
= null
;
if(suffix
.equals("xlsx")){
wb
= new XSSFWorkbook(ins
);
}else{
wb
= new HSSFWorkbook(ins
);
}
Sheet sheet
= wb
.getSheetAt(0);
if(null
!= sheet
){
for(int line
= 1; line
<= sheet
.getLastRowNum();line
++){
Row row
= sheet
.getRow(line
);
if(null
== row
){
continue;
}
row
.getCell(0).setCellType(Cell
.CELL_TYPE_STRING
);
String examti
= row
.getCell(0).getStringCellValue();
Calendar calendar
= new GregorianCalendar(1900,0,-1);
Date d
= calendar
.getTime();
Date examtime
= DateUtils
.addDays(d
,Integer
.valueOf(examti
);
SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd");
String timeFormat
= sdf
.format(examtime
);
Double sbp
= row
.getCell(1).getNumericCellValue();
Double dbp
= row
.getCell(2).getNumericCellValue();
Double bmi
= row
.getCell(3).getNumericCellValue();
Double whr
= row
.getCell(4).getNumericCellValue();
Double bun
= row
.getCell(5).getNumericCellValue();
Double ua
= row
.getCell(6).getNumericCellValue();
Double crea
= row
.getCell(7).getNumericCellValue();
Double tg
= row
.getCell(8).getNumericCellValue();
Double chol
= row
.getCell(9).getNumericCellValue();
Double glu
= row
.getCell(10).getNumericCellValue();
Double hcy
= row
.getCell(11).getNumericCellValue();
Double mAlb
= row
.getCell(12).getNumericCellValue();
Double MalbCrea
= row
.getCell(13).getNumericCellValue();
double teacherid
= row
.getCell(14).getNumericCellValue();
EaxmDTO eaxm
= new EaxmDTO();
eaxm
.setExamtime(timeFormat
);
eaxm
.setSbp(sbp
);
eaxm
.setDbp(dbp
);
eaxm
.setBmi(bmi
);
eaxm
.setWhr(whr
);
eaxm
.setBun(bun
);
eaxm
.setUa(ua
);
eaxm
.setCrea(crea
);
eaxm
.setTg(tg
);
eaxm
.setChol(chol
);
eaxm
.setGlu(glu
);
eaxm
.setHcy(hcy
);
eaxm
.setmAlb(mAlb
);
eaxm
.setmAlbCrea(MalbCrea
);
eaxm
.setTeacherid(teacherid
);
eaxmList
.add(eaxm
);
}
for(EaxmDTO eaxm
:eaxmList
){
result
= eaxmMapper
.insertEaxm(eaxm
);
}
}
return result
;
}
6.controller层如下
@RequestMapping("/importExam")
@ResponseBody
public String
importExam(@RequestParam MultipartFile
[] file
, HttpSession session
) {
int result
= 0;
try {
result
= eaxmService
.insertExam(file
[0]);
} catch (Exception e
) {
e
.printStackTrace();
}
if(result
> 0){
return "{\"result\":\"excel文件数据导入成功!\"}";
}else{
return "{\"result\":\"excel文件数据导入失败!\"}";
}
}
7.希望对各位有帮助,后面会更新图片上传+编辑的操作
各位大哥觉得好的给个赞