poi 文件导出 xls

版权声明:帅气Dee海绵宝宝 独家放送 https://blog.csdn.net/xyjcfucdi128/article/details/82347561

 前台下载跳转代码

$("#btn6").click(function() {
     window.open("http://localhost:8080/trainingNeeds/poi");
});
	

后台代码

/**
     * 功能描述:直接加载整个菜单树的数据(且必须要有管理员权限才可以加载该菜单树的数据) poi  导出
     * @return
     */
    @RequestMapping(value = "/poi",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String,Object> poi(@RequestBody QueryTrainingNeeds entity){
    	
    	System.out.println("=====================poi=======1");
    	Map<String,Object> result = new HashMap<String, Object>();
    	
        //创建 Excel 工作薄对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作表
        HSSFSheet sheet = workbook.createSheet("用户信息");
        //创建标题行
        HSSFRow row = sheet.createRow(0);
        String[] title = {"id","用户id","字典编码","建议","提交时间"};
        
        //创建单元格对象
        HSSFCell cell = null;
        for (int i = 0; i < title.length; i++) {
        //i 标示列索引
        cell = row.createCell(i);
        cell.setCellValue(title[i]);
        }
        
        //处理日期格式
        HSSFCellStyle cellStyle = workbook.createCellStyle(); //样式对象
        HSSFDataFormat dataFormat = workbook.createDataFormat(); //日期格式
        cellStyle.setDataFormat(dataFormat.getFormat("yyyy 年 MM 月 dd 日")); //设置日期格式
        
        //处理数据行
        List<TrainingNeeds> list =	getService().query(entity);//数据获取
        
        for (int i = 1; i < list.size(); i++) {
        	Date date = list.get(i).getSubmitTime();
        	
        	row = sheet.createRow(i);
            row.createCell(0).setCellValue(list.get(i-1).getId());
            row.createCell(1).setCellValue(list.get(i-1).getUserId());
            row.createCell(2).setCellValue(list.get(i-1).getDictCode());//字典编码
            row.createCell(3).setCellValue(list.get(i-1).getPropose());//建议
            //设置出生年月格式
            if(date!=null){ 
	             cell = row.createCell(4);
	             cell.setCellValue(date);//提交时间
	             cell.setCellStyle(cellStyle);
            }
			   
             
        try {
        	// workbook.write(response.getOutputStream());
        	 
        	 workbook.write(new File("e:\\用户反馈.xls"));
             workbook.close();
             System.out.println("==========response.getOutputStream()===============");
        	} catch (IOException e) {
        	// TODO Auto-generated catch block
        	e.printStackTrace();
        	}
        result.put(SystemStaticConst.RESULT,SystemStaticConst.SUCCESS);
		result.put(SystemStaticConst.MSG,"获取数据成功!");
        
        return result;
    }

猜你喜欢

转载自blog.csdn.net/xyjcfucdi128/article/details/82347561