POI读取excel表格

前台使用ajaxFileUpload上传文件,后台使用POI处理:

js代码:

function improtMessage() {
	$.messager.confirm('提示','确定您要导入配置?',function(r){   //jqueryEasyUI
		if (r){
			
			$.ajaxFileUpload({
				url: path+'/systemConfigureController/importSysConfig.do',
				secureuri: false,//异步
				fileElementId: 'file',//上传控件ID
				dataType: 'text',//返回的数据信息格式
				success: function(data){
				
				}
			})
		}
	});
}

后台接收和POI处理代码:
	private static int version2003 = 2003;  
    private static int version2007 = 2007;  
    private static int version = version2003;  
	/**
	 * 导入配置
	 * @param request
	 * @param response
	 * @throws IOException
	 */

	@RequestMapping("importSysConfig")
	public void importData(HttpServletRequest request, HttpServletResponse response) throws IOException{
		logger.info("**********into method importData*************");
		// 设置上下文
	    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
	        request.getSession().getServletContext());
	    // 检查form是否有enctype="multipart/form-data"
	    if (multipartResolver.isMultipart(request)) {
	    	MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
	    	Iterator<String> iter = multiRequest.getFileNames();
			List objList = new ArrayList();
	    	while (iter.hasNext()) {
	    		
	    		MultipartFile file = multiRequest.getFile(iter.next());
	    		logger.info("print out file:  "+file+"  fileName:  "+file.getName()+" fileSize: "+file.getSize());
    			version = (file.getName().endsWith(".xls") ? version2003 : version2007);
    			if (versio<pre class="java" name="code">

n == 2003) { POIFSFileSystem fs = new POIFSFileSystem(file.getInputStream()); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); <pre name="code" class="html"> objList = readCountryExcel(sheet);
 logger.info("print out readExcel: " + objList); }else if (version == 2007){ XSSFWorkbook hwk = new XSSFWorkbook(file.getInputStream());// 将is流实例到 一个excel流里 XSSFSheet sheet = hwk.getSheetAt(0);// 得到book第一个工作薄sheet objList = readCountryExcel(sheet); logger.info("print out readExcel: " + objList); } }//业务逻辑代码,调用service } response.getWriter().print("1");} 

 
public List<Country> readCountryExcel(Sheet sheet){
		List<Country> countryList = new ArrayList<Country>();
		 int rowNum = sheet.getPhysicalNumberOfRows();  //获得总共的行数
		 Country country = null;
		 for (int i =1; i<rowNum; i++){
			 country = new Country();
			 Row row = sheet.getRow(i);   //得到第i行数据
			 Cell c = null;
			 if (null != row){
				 c = row.getCell(0);
				 if (null != c){
					 if (c.getCellType() == c.CELL_TYPE_NUMERIC) {
						 c.setCellType(c.CELL_TYPE_STRING);
						 country.setCountry(c.getStringCellValue());
					 }else{
						 country.setCountry(c.getStringCellValue());
					 }
				 }
				 c = row.getCell(1);
				 if (null != c){
					 if (c.getCellType() == c.CELL_TYPE_NUMERIC) {
						 c.setCellType(c.CELL_TYPE_STRING);
						 country.setProvince(c.getStringCellValue());
					 }else {
						 country.setProvince(c.getStringCellValue());
					 }
				 }
				 c = row.getCell(2);
				 if (null != c){
					 if (c.getCellType() == c.CELL_TYPE_NUMERIC) {
						 c.setCellType(c.CELL_TYPE_STRING);
						 country.setZone(c.getStringCellValue());
					 }else {
						 country.setZone(c.getStringCellValue());
					 }
				 }
				 c = row.getCell(3);
				 if (null != c){
					 if (c.getCellType() == c.CELL_TYPE_NUMERIC) {
						 c.setCellType(c.CELL_TYPE_STRING);
						 country.setCode(c.getStringCellValue());
					 }else {
						 country.setCode(c.getStringCellValue());
					 }
				 }
			 }
			 countryList.add(country);
			 logger.info("print out read Excel row" + i + "Country: " + country);
		 }
		return countryList;
	}
	
需要引入的jar:poi-3.13-beta1.jar    poi-ooxml-3.13-beta1.jar   poi-ooxml-schemas-3.13-beta1.jar

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值