Action层-------Service层
/**
* 导入用户。
*
* @param request
* HttpServletRequest 用来存储和获取数据。
* @param response
* HttpServletResponse 用来响应数据信息。
* @return 重定向到展示用户列表。
*/
@RequestMapping("/importUser")
public void importUser(HttpServletRequest request,MultipartFile fileName, HttpServletResponse response) {
try {
// 1. 从流中获取文件数据
InputStream inputStream = fileName.getInputStream();
// 读取文件数据
ParseExcelForUser.parseAndImportUser(inputStream, userService); // 通过工具类保存用户。
} catch (IOException e) {
e.printStackTrace();
}
}
解析导入用户的工具类
package com.baizhi.util;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.baizhi.service.UserService;
import com.baizhi.entity.User;
/**
* 解析导入用户的工具类
* @author yjl
*/
public class ParseExcelForUser {
/**
* 解析导入用户的方法。getCellValue
*/
public static void parseAndImportUser(InputStream inputStream,UserService userService){
try {
// 将流中数据实例化为 HSSFWorkbook。
HSSFWorkbook wb = new HSSFWorkbook(inputStream);
// 获取sheet工作簿
HSSFSheet sheet = wb.getSheetAt(0);
int hang = 0;
// 获取表中所有行
hang = sheet.getPhysicalNumberOfRows();
for (int h = 1; h < hang; h++) {
// 获得工作表的行(用户)。
User ul = new User();
// 获取当前行数据
HSSFRow row = sheet.getRow(h);
String name = GetCellValue.getCellValue(row, 0);
String nickName = GetCellValue.getCellValue(row, 1);
String sex = GetCellValue.getCellValue(row, 2);
String image = GetCellValue.getCellValue(row, 3);
String sheng = GetCellValue.getCellValue(row, 5);
String shi = GetCellValue.getCellValue(row, 6);
String telphone = GetCellValue.getCellValue(row, 7);
String password = GetCellValue.getCellValue(row, 8);
ul.setName(name);
ul.setNickname(nickName);
ul.setUserImage(image);
ul.setSex(sex);
ul.setUserSheng(sheng);
ul.setUserShi(shi);
ul.setTelphone(telphone);
ul.setPassword(password);
userService.insert(ul);
}
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.baizhi.util;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
public class GetCellValue {
/**
* 获取工作薄sheet单元格中的值
* (2003版本取值)
* @param row
* @param cellNum
* @return
* @author lx
*/
public static String getCellValue(HSSFRow row,int cellNum){
Cell cell = row.getCell(cellNum);
if(cell == null){
return null;
}
switch(cell.getCellType()){
case Cell.CELL_TYPE_BLANK:
return null;
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue()+"";
case Cell.CELL_TYPE_NUMERIC:
return cell.getNumericCellValue()+"";
default:
return null;
}
}
/**
* 获取工作薄sheet单元格中的值
* (重载,用于2007版本)
* @param row
* @param cellNum
* @return
* @author lx
*/
public static String getCellValue(XSSFRow row,int cellNum){
Cell cell = row.getCell(cellNum);
if(cell == null){
return null;
}
switch(cell.getCellType()){
case Cell.CELL_TYPE_BLANK:
return null;
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue()+"";
case Cell.CELL_TYPE_FORMULA:
return cell.getNumericCellValue()+"";
default:
return null;
}
}
}
上一篇:导出Excel