导入(用户)的工具类

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值