jsp部分:
js部分:
function loadFile(data) {
initTable1.modal1();
}
var initTable1={
modal1:function(){
models = layer.open({
type : 1,
shade : 0.6,
shadeClose : true,
maxmin : true,
anim : 3,
title : '上传文件',
area : [ '30%', '30%' ], // 宽高
success:function(layero, index){
$("#remark").val("");
},
content : $('#promise_info_model1')
});
}
}
function FileUpLoad() {
var fileMsg = $("#file").val();
var ii = null;
$.ajaxFileUpload({
url : ctx + "/pls/oaManage/AuthorityImport.in",//用于文件上传的服务器端请求地址
secureuri : false, //是否需要安全协议,一般设置为false
fileElementId : 'file', //文件上传域的ID
dataType : 'text',
beforeSubmit : function() {
ii = layer.load(1);
},
success : function(data, status) //服务器成功响应处理函数
{
data = data.replace("
", ''); //ajaxFileUpload会对服务器响应回来的text内容加上", '');text前后缀
data = data.replace("
", '');", '');
data = data.replace("
后台代码:
/**
* Excel读流 直接上传到数据库
* 2018-02-26 Yangzb
* @return
*/
@RequestMapping(value = "AuthorityImport.in")
@ResponseBody
public String AuthorityImport(HttpServletRequest request, HttpServletResponse response,@RequestParam MultipartFile file) {
String err ="";//返回的提示
List
logger.debug("Excel导入开始");
// 设置响应给前台内容的数据格式
response.setContentType("text/plain; charset=UTF-8");
String fileName = "";
String realPath = "F://";
if (file != null && !file.isEmpty()) {
fileName = file.getOriginalFilename();
logger.debug("========================================");
logger.debug("file不为空!文件名称为"+fileName+"realPath为"+realPath);
}else{
return "上传失败!请选择正确文件。";
}
try {
//这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉
//此处也可以使用Spring提供的MultipartFile.transferTo(File dest)方法实现文件的上传
//将本地文件上传至服务器上
//FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath, fileName));
billManage.excelToDB(realPath,fileName, file);
} catch (Exception e) {
logger.debug("导入失败", e);
System.out.println("文件[" + file.getOriginalFilename() + "]上传失败,堆栈轨迹如下");
return "上传失败!请检查导入数据。";
}
logger.debug("导入信息跳转结束");
return "success";
}
}
/**
* Excel读流直接上传到数据库
* 2018-02-26 Yangzb
*/
@Override
public String excelToDB(String filePaht,String fileName,@RequestParam MultipartFile file) {
try {
List list =this.readExcel(filePaht,fileName,file);
} catch (ParseException e) {
e.printStackTrace();
}
return "success";
}
public List readExcel(String realPath,String fileName,@RequestParam MultipartFile file) throws ParseException {
List excelList = new ArrayList();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
try {
//InputStream is =new FileInputStream(realPath+"/"+fileName);
// jxl提供的Workbook类
Workbook wb = Workbook.getWorkbook(file.getInputStream());
// Excel的页签数量
// int sheet_size = wb.getNumberOfSheets();
// 每个页签创建一个Sheet对象
Sheet sheet = wb.getSheet(0);
// sheet.getRows()返回该页的总行数
for (int i = 1; i < sheet.getRows(); i++) {
// sheet.getColumns()返回该页的总列数
PlsCollRec plsCollRec = new PlsCollRec();
//如果第一行第一列为空的话不录入改行
if(StringUtils.isBlank(sheet.getCell(1,i).getContents())){
continue;
}
//for (int j = 0; j < sheet.getColumns(); j++) {
plsCollRec.setCaseId(sheet.getCell(0, i).getContents());
plsCollRec.setActionCode(sheet.getCell(1, i).getContents());
plsCollRec.setCollResult(sheet.getCell(2, i).getContents());
plsCollRec.setRelationship(sheet.getCell(3, i).getContents());
plsCollRec.setContactName(sheet.getCell(4, i).getContents());
plsCollRec.setTelNo(sheet.getCell(5, i).getContents());
plsCollRec.setCollTime(df.parse(sheet.getCell(6, i).getContents()));
plsCollRec.setPromiseAmt(new BigDecimal(sheet.getCell(7, i).getContents()));
plsCollRec.setPromiseDate(df.parse(sheet.getCell(8, i).getContents()));
plsCollRec.setCollOrg(sheet.getCell(9, i).getContents());
plsCollRec.setCollector(sheet.getCell(10, i).getContents());
plsCollRec.setCreateUser(sheet.getCell(11, i).getContents());
plsCollRec.setRemark("委外催记导入-"+sheet.getCell(12, i).getContents());
//}
excelList.add(plsCollRec);
rPlsCollRec.save(excelList);
}
//关闭流
//is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return excelList;
}
}