利用EXCEL文件批量上传

https://blog.csdn.net/femalcoder/article/details/55657030

 

ajax实现文件上传

 

https://blog.csdn.net/xxkalychen/article/details/77844542

 

 

@RequestMapping({ "/batchAdd" })
public R downBatchAddFile(HttpServletRequest request, HttpServletResponse response,
@RequestParam("file") MultipartFile file) throws IOException {
System.err.println(666);


List<ContentsEntity> list = new ArrayList<ContentsEntity>();

 

 

HSSFWorkbook book = new HSSFWorkbook(file.getInputStream());
HSSFSheet sheet = book.getSheetAt(0); // 得到工作簿的第一个sheet
int lastRowNum = sheet.getLastRowNum(); // 得到有数据的最大行数
System.out.println("行数:" + lastRowNum);
// 一般excel第一行是标题
HSSFRow row = null;
for (int i = 1; i <= lastRowNum; i++) {

row = sheet.getRow(i); // 得到工作表的第 i行
int type = excelUtil.getIntCellValue(row, 0);
String contentEn = excelUtil.getCellValue(row, 1);
String contentCn = excelUtil.getCellValue(row, 2);
String name = excelUtil.getCellValue(row, 3);
String img = excelUtil.getCellValue(row, 4);
String tips = excelUtil.getCellValue(row, 5);
String indications = excelUtil.getCellValue(row, 6);

String initial_sentence_en = excelUtil.getCellValue(row, 7);
String initial_sentence_cn = excelUtil.getCellValue(row, 8);
String replace_sentence_en = excelUtil.getCellValue(row, 9);
String replace_sentence_cn = excelUtil.getCellValue(row, 10);
String initial_vocabulary = excelUtil.getCellValue(row, 11);
String replace_vocabulary = excelUtil.getCellValue(row, 12);
String LabelDiagram = excelUtil.getCellValue(row, 13);
int time = excelUtil.getIntCellValue(row, 14);

if (type* contentEn.length() * contentCn.length() * name.length() * img.length()
* tips.length() * indications.length() * initial_sentence_en.length()
* initial_sentence_cn.length() * replace_sentence_en.length() * replace_sentence_cn.length()
* initial_vocabulary.length() * replace_vocabulary.length() * LabelDiagram.length()
* time != 0) {
ContentsEntity c = new ContentsEntity(0, type+"", contentEn, contentCn, name, img, tips, indications,
initial_sentence_en, initial_sentence_cn, replace_sentence_en, replace_sentence_cn,
initial_vocabulary, replace_vocabulary, LabelDiagram, time+"");
list.add(c);
}
}


// 解析excel成功,批量添加数据
System.out.println(list);
boolean result = contentsService.insertBatch(list);
if (result == true) {
return R.ok();
} else {
return R.error();
}

 

}

 

 

 

js 

 

batchSave: function(){


var formData = new FormData($('#uploadForm')[0]);
$.ajax({
type: 'post',
url: baseURL + "sys/contents/batchAdd",
data: formData,
cache: false,
processData: false,
contentType: false,
}).success(function (r) {
if(r.code==0){
alert('上传成功');
}
}).error(function () {
alert("上传失败");
});


}

转载于:https://www.cnblogs.com/ywb2018/p/9916156.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值