利用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