@RequestMapping(value = "/****", method = RequestMethod.POST)
public void import(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws Exception {
DeviceInfoExample example = new DeviceInfoExample();
List<DeviceInfo> dinfo = deviceService.selectDevice(example);
// 如果文件不为空,写入上传路径
if (!file.isEmpty()) {
// 上传文件路径
String fileName = "D:/new/" + file.getOriginalFilename();
file.transferTo(new File(fileName));
String[] split2 = fileName.split("\\\\");
String Str = "";
for (int i = 0; i < split2.length; i++) {
if (i < split2.length - 1) {
Str += split2[i] + "/";
} else {
Str += split2[i];
}
}
String filename = new String(Str.getBytes(), "utf-8");
InputStream stream = new FileInputStream(filename);
Workbook wb = null;
if (filename.endsWith("xls")) {
wb = new HSSFWorkbook(stream);
} else if (filename.endsWith("xlsx")) {
wb = new XSSFWorkbook(stream);
} else {
System.out.println("您输入的excel格式不正确");
}
Sheet sheet1 = wb.getSheetAt(0);
String meme = "";
for (Row row : sheet1) {
DeviceInfo device = new DeviceInfo();
for (Cell cell : row) {
cell.setCellType(Cell.CELL_TYPE_STRING);
meme += cell.getStringCellValue() + "--";
}
String[] split = meme.split("--");
meme = "";
if (split[0].equals("序号")) {
continue;
}
Integer routeid = 0;
Float lon = null ;
Float lat = null ;
for (DeviceInfo ds : dinfo) {
if (ds.getDevicename().equals(split[2])) {
routeid = ds.getRouteid();
lon = ds.getLongitude();
lat = ds.getLatitude();
}
}
// int id = car.size()+Integer.parseInt(split[0]); // 每行数据的id
// carInfo.setId(id);
SimpleDateFormat time1 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = time1.parse(split[7]);
device.setRouteid(routeid);
...
if (split[10].equals("正在工作")) {
device.setUsestatus(0);
} else {
device.setUsestatus(1);
}
deviceMapper.insert(device);
}
}
}
表格导入
最新推荐文章于 2019-11-27 14:10:44 发布