@RequestMapping(value="/import")
//导入
public String impot(@RequestParam MultipartFile[] file,HttpServletRequest request) throws IOException{
for(MultipartFile myfile : file){
if(myfile.isEmpty()){
System.out.println("文件未上传");
}else{
System.out.println("文件长度: " + myfile.getSize());
System.out.println("文件类型: " + myfile.getContentType());
System.out.println("文件名称: " + myfile.getName());
System.out.println("文件原名: " + myfile.getOriginalFilename());
System.out.println(request.getSession().getServletContext());
System.out.println("========================================");
//如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
//这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename()));
try {
InputStream input = new FileInputStream(realPath+"\\"+myfile.getOriginalFilename());
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println("Row #" + row.getRowNum());
Iterator<Cell> cells = row.cellIterator();
/*System.out.println(row.getCell(0).getStringCellValue()+row.getCell(1).getStringCellValue()+row.getCell(2).getStringCellValue()+row.getCell(3).getNumericCellValue());*/
System.out.println(row.getCell(0)+" "+row.getCell(1)+" "+row.getCell(2)+" "+row.getCell(3));
User user = new User();
//user.setUid((long)row.getCell(0).getNumericCellValue());
if(!row.getCell(1).toString().equals("姓名")&&!row.getCell(2).toString().equals("密码")&&!row.getCell(3).equals("年龄")){
user.setUsername(row.getCell(1).toString());
user.setPassword(row.getCell(2).toString());
user.setAge((long)row.getCell(3).getNumericCellValue());
userService.save(user);
}
/*while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
System.out.println("Cell #" + cell.getCellNum());
String username;
String password;
Long a;
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
default:
System.out.println("unsuported sell type");
break;
}
}*/
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
return WebUtil.convertToJsonData("success");
}
//导入
public String impot(@RequestParam MultipartFile[] file,HttpServletRequest request) throws IOException{
for(MultipartFile myfile : file){
if(myfile.isEmpty()){
System.out.println("文件未上传");
}else{
System.out.println("文件长度: " + myfile.getSize());
System.out.println("文件类型: " + myfile.getContentType());
System.out.println("文件名称: " + myfile.getName());
System.out.println("文件原名: " + myfile.getOriginalFilename());
System.out.println(request.getSession().getServletContext());
System.out.println("========================================");
//如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
//这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename()));
try {
InputStream input = new FileInputStream(realPath+"\\"+myfile.getOriginalFilename());
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println("Row #" + row.getRowNum());
Iterator<Cell> cells = row.cellIterator();
/*System.out.println(row.getCell(0).getStringCellValue()+row.getCell(1).getStringCellValue()+row.getCell(2).getStringCellValue()+row.getCell(3).getNumericCellValue());*/
System.out.println(row.getCell(0)+" "+row.getCell(1)+" "+row.getCell(2)+" "+row.getCell(3));
User user = new User();
//user.setUid((long)row.getCell(0).getNumericCellValue());
if(!row.getCell(1).toString().equals("姓名")&&!row.getCell(2).toString().equals("密码")&&!row.getCell(3).equals("年龄")){
user.setUsername(row.getCell(1).toString());
user.setPassword(row.getCell(2).toString());
user.setAge((long)row.getCell(3).getNumericCellValue());
userService.save(user);
}
/*while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
System.out.println("Cell #" + cell.getCellNum());
String username;
String password;
Long a;
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
System.out.println(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
System.out.println(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
default:
System.out.println("unsuported sell type");
break;
}
}*/
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
return WebUtil.convertToJsonData("success");
}