简单点,直接上图
<input style="height: 24px;position: relative;float: left;display:none;top: 4px;left: 41px;" type="text" placeholder="点击选择要上传的文件" readonly="readonly" value="${fileName}" name="fileName">
<!-- <button style="height: 30px;width:120px;margin-top: -2px;background-color:#1C8CC9;">导入Excel</button> -->
<div style="display:inline-block; vertical-align:middle;position: absolute;z-index: 1;top:5px;left: 325px;">
<div id="doc_pic_up" data-toggle="upload"
data-uploader="co/coTaxDisc/upload.do"
data-file-size-limit="1024000"
data-file-type-exts="*.xls;*.xlsx"
data-multi="true"
data-auto="true"
data-multiup="true"
data-file-obj-name="file"
data-on-upload-success="coTaxDisc.upload_success"
data-button-text="导入Excel"
data-button-width="100px"></div>
</div>
/**
* excel解析
* TODO 修改此处注释
* @param files
* @throws InvalidFormatException
*/
@RequestMapping("/upload")
public ModelAndView upload(@RequestParam(value = "file", required = false) MultipartFile file ,String cup_id) throws InvalidFormatException {
try {
if(cup_id==null || "".equals(cup_id)){
throw new ServiceException("未选择目标机柜");
}
CoCupboardModel coCupboardModel = coCupboardService.get(Long.valueOf(cup_id));
//解析excel前清空表()
//coBatchImportService.deleteCoBatchImportAll();
//清除临时表中当前机柜的记录
boolean deleteResult = coBatchImportService.deleteCoBatchImportByCupId(cup_id);
String operaterid = SessionUtils.getUser().getId() + "";
String fileName = file.getOriginalFilename();
int rowNum = 0;
int tNum = 0;
InputStream is = file.getInputStream();
Workbook hssfWorkbook =WorkbookFactory.create(is);
CoBatchImportModel model = new CoBatchImportModel();
List<CoBatchImportModel> list = new ArrayList<CoBatchImportModel>();
boolean success;
boolean allsuccess = true;
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
if(numSheet == 0){
// int a = hssfSheet.getLastRowNum();
// int b = hssfSheet.getPhysicalNumberOfRows();
for (rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
Row hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
if(ExcelUtil.getCellValue(hssfRow.getCell(0)) != null && !ExcelUtil.getCellValue(hssfRow.getCell(0)).equals("")){
tNum ++ ;
model = new CoBatchImportModel();
model.setSide(ExcelUtil.getCellValue(hssfRow.getCell(0)));
model.setPos(ExcelUtil.getCellValue(hssfRow.getCell(1)));
model.setCustomer(ExcelUtil.getCellValue(hssfRow.getCell(2)));
model.setTax_no(ExcelUtil.getCellValue(hssfRow.getCell(3)));
model.setQrcode(ExcelUtil.getCellValue(hssfRow.getCell(4)));
model.setIp(ExcelUtil.getCellValue(hssfRow.getCell(5)));
model.setMac(ExcelUtil.getCellValue(hssfRow.getCell(6)));
model.setCup_id(cup_id);
model.setCup_name(coCupboardModel.getName());
model.setOperater(Long.valueOf(operaterid));
list.add(model);
success = coBatchImportService.saveCoBatchImport(model);
if(!success){
allsuccess=false;
}
}
}
}
}
}
if(allsuccess) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("cup_id", cup_id);
// result.put("list", list);
sendSuccessMessage("Excel解析成功!",result);
return forword(viewPath + "coTaxDisc_batchimport");
}
else{
sendFailureMessage("Excel解析失败,重新尝试!");
return forword(viewPath + "coTaxDisc_batchimport");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
后台涉及需求功能,不必与我完全相同,仅作为记录。