<body>
<div class="form-group col-sm-10" id="thumbnailUploadContainer" style="float: left; margin-right: 50px;">
<input id="articleImageFile" name="excelFile" type="file" class="form-control" style="width: 300px; display: inline;" />
<input id="saveZipButton" type="button" style="width: 60px;height: 35px;" value="上传" />
</div>
</body>
.html
<script th:inline="javascript" type="text/javascript">
$(function() {
$("#saveZipButton").on('click', function(){
var formData = new FormData();
var name = $("#articleImageFile").val();
formData.append("file",$("#articleImageFile")[0].files[0]);
formData.append("name",name);//这个地方可以传递多个参数
$.ajax({
url : BASE_CTX_PATH + "business/reportform/getImportDistrictList",
type : 'POST',
async : false,
data : formData,
// 告诉jQuery不要去处理发送的数据
processData : false,
// 告诉jQuery不要去设置Content-Type请求头
contentType : false,
beforeSend:function(){
console.log("正在进行,请稍候");
},
success : function(responseStr) {
if(responseStr=="success"){
alert("导入成功");
}else{
alert("导入失败");
}
}
});
});
});
</script>
.js
@PostMapping(value = "/getImportDistrictList")
@ResponseBody
public String export(
@RequestParam("file") MultipartFile file,
HttpServletRequest request, HttpServletResponse response) {
return reportFormService.export(file,request,response);
}
controller
@Override
public String export(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
try {
// @RequestParam("file") MultipartFile file 是用来接收前端传递过来的文件
// 1.创建workbook对象,读取整个文档
InputStream inputStream = file.getInputStream();
//2013版及之前的
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream);
HSSFWorkbook wb = new HSSFWorkbook(poifsFileSystem);
// 2.读取页脚sheet
HSSFSheet sheetAt = wb.getSheetAt(0);
// 3.循环读取某一行
for (Row row : sheetAt) {
// 4.读取每一行的单元格
String partNo = row.getCell(0).getStringCellValue(); // 第一列数据
String madeIn= row.getCell(2).getStringCellValue(); // 第三列数据
// double doubleCellValue2 = row.getCell(1).getNumericCellValue();// 第二列
// 写多少个具体看大家上传的文件有多少列.....
// 测试是否读取到数据,及数据的正确性
logger.info(partNo+" "+madeIn);
int result = reportFormMapper.insertMadeIn(partNo,madeIn);
if(result!=0) {
return "success";
}else {
return "fail";
}
}
return "success";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "fail";
}
}
service