jxls导出功能代码

jxls导出功能

jsp页面

		<shiro:hasPermission name="rgssys:projectsInfo:import">
				<table:importExcel url="${ctx}/rgssys/projectsInfo/import"></table:importExcel><!-- 导入按钮 -->
			</shiro:hasPermission>
			<shiro:hasPermission name="rgssys:projectsInfo:import">
				<button οnclick="importManmonthInfos()" class="btn btn-white btn-sm" data-toggle="tooltip" data-placement="left" title="导入工时"><i class="fa fa-folder-open-o"></i>导入工时</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="rgssys:projectsInfo:export">
	       		<table:exportExcel url="${ctx}/rgssys/projectsInfo/export"></table:exportExcel><!-- 导出按钮 -->
	       	</shiro:hasPermission>
	     
controller层
	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("rgssys:projectsInfo:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public void exportFile(ProjectsInfo projectsInfo, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String year = request.getParameter("year");
			if(StringUtils.isBlank(year)){
				year = DateUtils.getDate("yyyy");
			}
			String fileName = year + "年项目人员信息表";
            Page<ProjectsInfo> page = projectsInfoService.findPage(new Page<ProjectsInfo>(request, response, -1), projectsInfo);
            XLSTransformer transformer = new XLSTransformer();
			Map<String , Object> model = new HashMap<String , Object>();				
			model.put("list", this.getProjectData(page.getList(), year));
			
			List<ManmonthInfo> manmonthInfos = new ArrayList<ManmonthInfo>();
			List<ProjectMemberInfo> memberInfos = new ArrayList<ProjectMemberInfo>();
			for(ProjectsInfo info : page.getList()){
				memberInfos.addAll(projectsInfoService.get(info.getId()).getProjectMemberInfoList());
				info.setYear(String.valueOf(year));
				manmonthInfos.addAll(projectsInfoService.getManmonthInfoList(info));
			}
			//人员投入状况
			model.put("personTypeList", this.getDictNum("personType", memberInfos, Integer.parseInt(year)));
			//人员保有状况
			model.put("personLevelList", this.getPersonNum("job", Integer.parseInt(year)));
			//工数状况
			Map<String, Map<String, Double>> manDayDataMap = this.getManDayData(manmonthInfos);
			model.put("pMap", manDayDataMap.get("pMap"));//预想
			model.put("rMap", manDayDataMap.get("rMap"));//实际
			model.put("mMap", manDayDataMap.get("mMap"));//投入
			String templateFileName = request.getServletContext().getRealPath("/") + "/WEB-INF/model/projectInfo.xlsx";
			InputStream is = new BufferedInputStream(new FileInputStream(templateFileName));
			Workbook workbook = transformer.transformXLS(is, model);
			this.saveExcelFile(workbook, fileName, response);
		} catch (Exception e) {
			e.printStackTrace();
			addMessage(redirectAttributes, "导出项目信息失败!失败信息:"+e.getMessage());
		}
    }
	
	public void saveExcelFile(Workbook workbook, String xlsTemplateName, HttpServletResponse response) {  
        //设置导出弹出框,以及下载文件名称  
		response.setContentType("application/octet-stream; charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename="+Encodes.urlEncode(xlsTemplateName)+".xlsx");  
        OutputStream os = null;  
        try {  
            os = response.getOutputStream();  
            workbook.write(os);  
            os.flush();    
            os.close();    
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
	}  
	


如果控制台报如下信息   的话   就是需要把模板文件后缀改成 2003版本 以 xls结尾的Excel  代码里面的xlsx替换为xls就好了

[2017-07-25 17:47:20][DEBUG][net.sf.jxls.tag.ForEachTag.process(ForEachTag.java:178)]Current tagContext: Beans: {workbook=Name: /xl/workbook.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml, hssfRow=<xml-fragment r="3" spans="1:8" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:main="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <main:c r="A3" t="s">
    <main:v>9</main:v>
  </main:c>
</xml-fragment>, hssfCell=null, fileName=程序猿被投票者得分统计表, list=[com.weidingplus.mall.web.mybatis.pojo.WdpVoteCandidate@186e252, com.weidingplus.mall.web.mybatis.pojo.WdpVoteCandidate@74a055, com.weidingplus.mall.web.mybatis.pojo.WdpVoteCandidate@ca4b75, com.weidingplus.mall.web.mybatis.pojo.WdpVoteCandidate@11ff4e2], sheet=Name: /xl/worksheets/sheet1.xml - Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml}, Body: Block (2, -1, 4, -1)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值