欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

简单的excel导出数据详细步骤

程序员文章站 2024-01-05 20:57:22
...

导出excel数据

/**
* 前台使用如下格式
*/
window.location.href=url+/**/download?condition="+filesAccess
/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/download")
    public void download(HttpServletRequest request,HttpServletResponse response){
		String condition=request.getParameter("condition");
		System.out.println("------------condition:"+condition);
		
		Map<String,Object> params=JsonUtils.JSONToMap(condition);
		System.out.println("-------------------consOrgNo:"+params.get("consOrgNo"));
		List<Map<String, Object>> lists = StatisticalDataImpl.getBusiWorkInfo(params);
		if(lists==null || lists.size()==0){
			System.out.println("--------当前条件下无导出数据----------");
			return;
		}
		System.out.println("---------lists.size():"+lists.size());
		//导出
		this.downloadExcelData(lists,request,response);
	}



/**
	 * 导出excel数据
	 */
	public void downloadExcelData(List<Map<String, Object>> lists,HttpServletRequest request,HttpServletResponse response){
		// 第一步,创建一个webbook,对应一个Excel文件  
        HSSFWorkbook wb = new HSSFWorkbook();  
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet  
        HSSFSheet sheet = wb.createSheet("未收集资料营销工单统计1");
        //设置固定例
        sheet.setDefaultColumnWidth(20);
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
        HSSFRow row = sheet.createRow((int) 0);  
        // 第四步,创建单元格,并设置值表头 设置表头居中  
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("供电单位");  
        cell.setCellStyle(style);  
        cell = row.createCell(1);  
        cell.setCellValue("业务类");  
        cell.setCellStyle(style);  
        cell = row.createCell(2);  
        cell.setCellValue("业务类型");  
        cell.setCellStyle(style);  
        cell = row.createCell(3);  
        cell.setCellValue("申请编号");
        cell.setCellStyle(style);  
        cell = row.createCell(4);  
        cell.setCellValue("用户编号");  
        cell.setCellStyle(style);  
        cell = row.createCell(5);  
        cell.setCellValue("用户名称");  
        cell.setCellStyle(style);
        cell = row.createCell(6);  
        cell.setCellValue("申请时间");  
        cell.setCellStyle(style);
        cell = row.createCell(7);  
        cell.setCellValue("归档时间");  
        cell.setCellStyle(style);
        cell = row.createCell(8);  
        cell.setCellValue("业务流程状态");  
        cell.setCellStyle(style);
        
        // 第五步,创建单元格,并设置值  
        for (int i = 0; i < lists.size(); i++){  
            row = sheet.createRow((int) i + 1);
            
            row.createCell(0).setCellValue(lists.get(i).get("orgName")+"");  
            row.createCell(1).setCellValue(lists.get(i).get("busiClass")+"");  
            row.createCell(2).setCellValue(lists.get(i).get("busiItemCode")+"");  
            row.createCell(3).setCellValue(lists.get(i).get("marketingAppNo")+"");  
            row.createCell(4).setCellValue(lists.get(i).get("consNo")+"");  
            row.createCell(5).setCellValue(lists.get(i).get("consName")+"");  
            row.createCell(6).setCellValue(lists.get(i).get("appTime")+"");  
            row.createCell(7).setCellValue(lists.get(i).get("finishTime")+""); 
            row.createCell(8).setCellValue(lists.get(i).get("busiStatus")+""); 
        }

        //第六步,下载
        
        //时间创建文件名
        String fileName="未收集资料营销工单统计";
	    
	    BufferedOutputStream out=null;	
        try{
        	// 创建文件输出流,准备输出电子表格
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream"); 
            String agent=request.getHeader("User-Agent").toLowerCase();
        	if(agent.indexOf("firefox")>0){ 
    	    	response.addHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("GB2312"),"ISO-8859-1")+".xls");
    	    }else{
    	    	response.setHeader("Content-disposition", "attachment; filename="+ URLEncoder.encode(fileName,"UTF-8")+".xls"); 
    	    }
        	
        	out = new BufferedOutputStream(response.getOutputStream());
            wb.write(out);
            out.flush();
        }catch (IOException e){  
            e.printStackTrace();
        }finally{
        	try {
				out.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
	}

测试

简单的excel导出数据详细步骤

相关标签: java

上一篇:

下一篇: