生成excel文档

/**
	 * 删除excel表格
	 * @param pid
	 * @param str
	 * @param etr
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value="/deletefile.action")
	public String deletefile(Long pid) throws Exception{		
		JSONObject jsobjcet = new JSONObject();
		String json = null;
		try {
			Integer a = userOrderService.deleteFile(pid);
			if(a>0){
				jsobjcet.put("mm", "1");
			}else{
				jsobjcet.put("mm", "0");
			}
			
		} catch (Exception e) {
			logger.error(e.toString());
			jsobjcet.put("mm", "0");
		}
		json = JsonUtil.writeObject(jsobjcet);
		return json;
		
	}
	
	public String xlsDto2Excel(List<UserOrderDto> xls) throws Exception {
        // 获取总列数
	int CountColumnNum = 6;
        // 创建Excel文档
        HSSFWorkbook hwb = new HSSFWorkbook();
        UserOrderDto xlsDto = null;
        // sheet 对应一个工作页
        HSSFSheet sheet = hwb.createSheet("pldrxkxxmb");
        HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始
        HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
        String[] names = new String[6];
        names[0] = "姓名";
        names[1] = "产品名称";
        names[2] = "电话";
        names[3] = "地址";
        names[4] = "订购代码";
        names[5] = "备注";
        for (int j = 0; j < CountColumnNum; j++) {
            firstcell[j] = firstrow.createCell(j);
            firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
        }
        for (int i = 0; i < xls.size(); i++) {
            // 创建一行
            HSSFRow row = sheet.createRow(i + 1);
            // 得到要插入的每一条记录
            xlsDto = xls.get(i);
            for (int colu = 0; colu <= 4; colu++) {
                // 在一行内循环
                HSSFCell xh = row.createCell(0);
                xh.setCellValue(xlsDto.getUser_name());
                HSSFCell xm = row.createCell(1);
                xm.setCellValue("巴西比戈免费版");
                HSSFCell yxsmc = row.createCell(2);
                yxsmc.setCellValue(xlsDto.getMobile());
                HSSFCell kcm = row.createCell(3);
                kcm.setCellValue(xlsDto.getAddress());
                HSSFCell cj = row.createCell(4);
                cj.setCellValue(xlsDto.getAd_code());
                HSSFCell note = row.createCell(5);
                if(xlsDto.getOrder_note()==null){
                	xlsDto.setOrder_note("无");
                }
                note.setCellValue("备注内容:"+xlsDto.getOrder_note()+" 下单时间:"+xlsDto.getCreate_time_show());
                //(xlsDto.getMessage());
            }
        }
        Date date = new Date();
		Integer year = DateUtil.getYear(date);
		Integer month = DateUtil.getMonth(date);
		Integer day = DateUtil.getDayOfMonth(date);
		int ran = (int) (Math.random() * (10000 - 1000) + 1000);
		String mp = "/home/www/manage.kangban.org/";
		String path = year+"/"+month+"/"+day;
		java.io.File myFilePath = new java.io.File(mp+path);
        if (!myFilePath.exists()) {
          myFilePath.mkdirs();
        }
        // 创建文件输出流,准备输出电子表格
        OutputStream out = new FileOutputStream(mp+path+"/orderinfo"+ran+".xls");
        hwb.write(out);
        out.close();
        Integer a = userOrderService.saveExportFile("orderinfo"+ran+".xls",path+"/orderinfo"+ran+".xls");
        System.out.println("excel导出成功");
        return path+"/orderinfo"+ran+".xls";
    }
	/**
	 * 生成的excel文档列表
	 * @param request
	 * @param orderid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = ("/exportfile.action"))
	public ModelAndView getExportFile(HttpServletRequest request,HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView();
 
		List<ExportFileDto> list = userOrderService.getExportFileLisrt();
		 
		mv.addObject("list", list);
		mv.setViewName("/ordermanage/exportfile");
		mv.addObject("contentPath", getContentPath());
		return mv;
	}
	/**
	 * 导出excel文档
	 * @param pid
	 * @param str
	 * @param etr
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value="/getexcelbyids.action")
	public String excelByids(Long pid, String str,String etr) throws Exception{		
		JSONObject jsobjcet = new JSONObject();
		String json = null;
		try {
			List<UserOrderDto> list = userOrderService.getSelectedOrders(pid,str,etr);
			list = parseData(list);
			String path = xlsDto2Excel(list);
			for(int j=0;j<list.size();j++){
			userOrderService.updateOrderStatusByID(list.get(j).getOrder_id(),3);
			}
			jsobjcet.put("mm", "1");
			jsobjcet.put("path", path);
		} catch (Exception e) {
			logger.error(e.toString());
			jsobjcet.put("mm", "0");
			jsobjcet.put("path", "");
		}
		json = JsonUtil.writeObject(jsobjcet);
		return json;
		
	}

;