Excel导入数据库的前后端代码

1、前端页面:

<div id="div1" style="float: left; margin-right: 50px;">
<input id="articleImageFile" name="excelFile" type="file" style="width: 300px; display: inline;" />
<input id="saveZipButton" type="button" style="width: 60px;height: 35px;" value="上传" />
</div>

2、js:

$(document).ready(function() {

// 点击上传按钮保存excel
$("#saveZipButton").click(function() {
// 2020 新excel导入数据库事件
newreport();
});

});

// 2020 导入excel文件
function newreport() {
var fileName = $("#articleImageFile").val();
if (fileName == null || fileName == "") {
alert("请选择文件");
} else {
var fileType = fileName.substr(fileName.length - 4, fileName.length);
if (fileType == ".xls" || fileType == "xlsx") {

var formData = new FormData();
var name = $("#articleImageFile").val();
formData.append("file", $("#articleImageFile")[0].files[0]);
formData.append("name", name);// 这个地方可以传递多个参数
$.ajax({
url : getContextPathInfo() + "/execImport/upload",
type : 'POST',
async : false,
data : formData,
// 告诉jQuery不要去处理发送的数据
processData : false,
// 告诉jQuery不要去设置Content-Type请求头
contentType : false,
beforeSend : function() {
console.log("正在进行,请稍候");
},
success : function(json) {
var checkYwState = false;
console.info("OnSuccess_NonNeedOpe_del方法");
if (typeof (json) == "object") {
if (json.resulttype == "SUCCESS") {
alert("Excel导入数据成功");
nsearch();
var deldm = json.appenddata.dm;
} else {
alert("Excel导入数据出现错误");
return false;
}
} else {
alert("Excel导入数据出现异常");
return false;
}
// if (responseStr == "1") {
// alert("导入成功");
// } else {
// alert("导入失败");
// }
},
error : function(json){
alert("导入报错");
}
});
}else {
alert("上传文件类型错误!");
}
}
};

3、后台java控制层方法:

//excel导入
@RequestMapping(value = "/upload" , method = RequestMethod.POST)
@ResponseBody
@Log(module = "车辆信息Excel表导入",operation = "新增")
public OperationResult export(@RequestParam("file") MultipartFile file,
HttpServletRequest request, HttpServletResponse response) {
Integer iscuccess = 0;
OperationResult result = new OperationResult(OperationResultTypeEnum.ERROR, "作废记录失败");
try {
// @RequestParam("file") MultipartFile file 是用来接收前端传递过来的文件
// 1.创建workbook对象,读取整个文档
InputStream inputStream = file.getInputStream();
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream);
HSSFWorkbook wb = new HSSFWorkbook(poifsFileSystem);
// 2.读取页脚sheet
HSSFSheet sheetAt = wb.getSheetAt(0);
List<Zbclbaseinfob> zbclbaseinfobLst = new ArrayList<Zbclbaseinfob>();//用来装车辆实体的list
//*********************从第二行开始的excel导入(已成功)*******************
for(int rowNum = 1;rowNum <= sheetAt.getLastRowNum(); rowNum++ ){//从第二行开始取值(即rowNum=1开始)
//创建车辆实体对象
Zbclbaseinfob clEntity = new Zbclbaseinfob();
HSSFRow xssfRow = sheetAt.getRow(rowNum);
// 4.读取每一行的单元格
String stringCellValue = xssfRow.getCell(0).getStringCellValue(); // 第一列数据
String stringCellValue2 = xssfRow.getCell(1).getStringCellValue();// 第二列
clEntity.setDm(UUID.randomUUID().toString());//生成随机主键
clEntity.setClbh(stringCellValue);//给车辆编号赋值
clEntity.setCph(stringCellValue2);//给车牌号赋值
// 写多少个具体看大家上传的文件有多少列.....
zbclbaseinfobLst.add(clEntity);//
// execImportService.saveClExcel(clEntity);//循环去业务层单条保存车辆
// 测试是否读取到数据,及数据的正确性
//System.out.println(stringCellValue);
}
// execImportService.manyClSave(zbclbaseinfobLst);//saveAll车辆表list去业务层保存
iscuccess = execImportService.manyClSave(zbclbaseinfobLst);//saveAll车辆表list去业务层保存
if (iscuccess == 1) {
result.setResulttype(OperationResultTypeEnum.SUCCESS);
result.setMsg("Excel导入成功");
} else {
result.setResulttype(OperationResultTypeEnum.ERROR);
result.setMsg("Excel导入失败");
}
result.setAppenddata(iscuccess);
return result;

//*********************从第一行开始的excel导入(已成功)*******************
// // 3.循环读取某一行
// for (Row row : sheetAt) {
// //创建车辆实体对象
// Zbclbaseinfob clEntity = new Zbclbaseinfob();
// // 4.读取每一行的单元格
// String stringCellValue = row.getCell(0).getStringCellValue(); // 第一列数据
// String stringCellValue2 = row.getCell(1).getStringCellValue();// 第二列
// clEntity.setDm(UUID.randomUUID().toString());//生成随机主键
// clEntity.setClbh(stringCellValue);//给车辆编号赋值
// clEntity.setCph(stringCellValue2);//给车牌号赋值
// // 写多少个具体看大家上传的文件有多少列.....
// execImportService.saveClExcel(clEntity);
// // 测试是否读取到数据,及数据的正确性
// //System.out.println(stringCellValue);
// }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();//若一段代码前有异常抛出,并且这个异常被try...catch所捕获,若此时catch语句中没有抛出新的异常,则这段代码能够被执行
result.setResulttype(OperationResultTypeEnum.ERROR);
result.setMsg("Excel导入失败");
return result;
}

}

猜你喜欢

转载自www.cnblogs.com/xyg34/p/12362913.html
今日推荐