web数据导出到Excel的步骤方法及纠错(一)

页面HTML代码写法——添加到处Excel按钮

<input type="button" ng-click="exports0()" value="导出Excel">

selectController.js ——添加内容

//数据导出到excel
    $scope.exports0 = function () {
    	userService.exports0().success(
    			function (response) {
    				if(response.success)
    					alert("到处数据成功!!!");
    				else
    					alert("系统出错!!!");
    			}
    	);
    }

userService.js ——添加内容(该部分与javad代码相关联)

   this.exports0 = function(){
		window.location.href = "/PostalBank/user/findexcle.do" ;
	}

ScoreController.java 添加 (这是我添加的 你可以把他放到相应的controller中去)

@RequestMapping("findexcle.do")
	public Result exports0( HttpServletRequest request, HttpServletResponse response) {
		try {
			ServletOutputStream out = response.getOutputStream();
		String fileName = "劳动纪律评分.xls";
		String resultFileName = ImportExcelUtil.encodeChineseDownloadFileName(request, fileName);
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.addHeader("Content-Disposition", "attachment; filename=" + resultFileName);
		List<Map> list = userService.getscroe(new TUser());
		userService.export001(out,list);

	} catch (Exception e) {
		e.printStackTrace();
		return new Result(false, "导出失败");
	}
	return new Result(true, "导出成功");
}

UserService.java ——添加内容

export001(ServletOutputStream out, List<Map> list);
List<Map> getscroe(TUser tuser)

UserServiceImpl.java-----添加内容

@Override
    	public List<Map> getscroe(TUser tuser) {
    		TUserExample example  = new TUserExample();
    		example.setOrderByClause("id ASC");
    		TUserExample.Criteria criteria = example.createCriteria();
    		if(tuser != null) {
    			//按工号进行匹配搜索
    			if(tuser.getId()!= null) {
    				criteria.andIdEqualTo(tuser.getId());
    			}	
    		}
    		List<Map> list = tUserMapper.selectUserscroe(tuser);
    		return list;
    	}

@Override
public void export001(ServletOutputStream out, List<Map> list) {
		
// 第一步,创建一个workbook,对应一个Excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
       // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet hssfSheet = workbook.createSheet("劳动纪律");
       // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow hssfRow = hssfSheet.createRow(0);
   
        HSSFCell cell = null;
        
        String[] names = {"工号","姓名","公司","部门","岗位","评分"};
        for (int i = 0; i < names.length; i++) {
      	  
      	  //为了每列设置单元格的颜色,得创建多个单元格格式对象
            HSSFCellStyle hssfCellStyle = workbook.createCellStyle();
             //居中样式
            hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            HSSFCell hssfCell = null;
      	    hssfCell = hssfRow.createCell(i);//列索引从0开始
            hssfCell.setCellValue(names[i]);//列名1
              
              //设置字体样式
              HSSFFont  fontStyle=workbook.createFont(); 
              fontStyle.setFontName("黑体");  
              //添加到cell样式中
              hssfCellStyle.setFont(fontStyle);
        	  hssfCell.setCellStyle(hssfCellStyle);//列居中显示       
		  }
        
        for(int j =0 ;  j< list.size();j++) {
        	Map map = list.get(j);
        	HSSFRow row = hssfSheet.createRow(j+1);
        	
        	//可以使用遍历表头进行填充 但是获取内容类型必须一致才可以 例如都是String 类型
        	cell = row.createCell(0);
        	//这里对空指针异常的处理  有的字段空值 他在map中没有key 
        	//那我咋知道他有没有key了,下面这两段的区别?
        
        	//	cell.setCellValue(map.get("jobnumber").toString() != null ? map.get("jobnumber").toString() : null);
        	cell.setCellValue(!StringUtils.isEmpty(map.get("jobnumber")+"")  ? map.get("jobnumber").toString() : null);
        	cell = row.createCell(1);
        	cell.setCellValue(!StringUtils.isEmpty(map.get("nickname")+"")  ? map.get("nickname").toString() : null);
        	cell = row.createCell(2);
        	cell.setCellValue(!StringUtils.isEmpty(map.get("companyname")+"")  ? map.get("companyname").toString() : null);
        	cell = row.createCell(3);
        	cell.setCellValue(!StringUtils.isEmpty(map.get("categoryname")+"")  ? map.get("categoryname").toString() : null);
        	cell = row.createCell(4);
        	cell.setCellValue(!StringUtils.isEmpty(map.get("deptname")+"")  ? map.get("deptname").toString() : null);
        	cell = row.createCell(5);
        	cell.setCellValue(map.containsKey("labourscore") && !StringUtils.isEmpty(map.get("labourscore")+"")  ? map.get("labourscore").toString() : null);
        	
        }
        
        // 第七步,将文件输出到客户端浏览器
       try {
            workbook.write(out);
            out.flush();
            out.close();
       
         } catch (Exception e) {
            e.printStackTrace();
         }
		
		
	}

TuserMapper.xml

	<select id="selectUserscroe" resultMap="BaseResultMapToMap" parameterType="com.shenshou.postalbank.dao.pojo.TUserExample" >
    select 
    <include refid="Base_Column_List" />
    from t_user
  </select>

这些步骤写完,页面数据导出EXCEL就能够实现了。当然在这其中我们也会遇到一些各种各样的问题。比如我在通过修改这部分代码时候放在其他页面上面,会出现导出的数据为乱码。当这个问题出现,我的解决及错误将向大家展示如下。
web数据导出到Excel的步骤方法及纠错(二)https://blog.csdn.net/baidu_38878945/article/details/84447037

猜你喜欢

转载自blog.csdn.net/baidu_38878945/article/details/84446156