將數據導入Excel

/**
    * 查詢未打印訂單
    * @param req
    * @param sort
    * @param order
    * @param rows
    * @param page
    * @return
    */
   public JSONObject queryOrder(HttpServletRequest req,String startDate,String endDate){
       Date date = new Date();
       DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
       String time=format.format(date);    //得到當前時間
       StringBuffer sql1 = new StringBuffer("select ifnull(u.parentUserCode,'')parentUserCode,"
                  + "ifnull(u.siteName,'')siteName,ifnull(e.expressNO,'')expressNO,"
                 + "ifnull(date_format(e.takeDate,'%Y-%m-%d %H:%m:%s'),'')takeDate,"
                 + "ifnull(date_format(e.enterDate,'%Y-%m-%d %H:%m:%s'),'')enterDate,"
                 + "from express e join loginuser u "
                 + "where e.loginUserCode = u.userCode  and  e.STATUS = 1 ");
      
       if((startDate != null && !"".equals(startDate)) && (endDate != null && !"".equals(endDate))){
           sql1.append(" and e.takeDate between '"+startDate+"' "+" and "+" '" + endDate+"'"); 
       }  else{
          sql1.append(" and e.takeDate like '%"+time+"%' ");   //導出當前時間的數據
      }
      
       List<Map<String, Object>> list = jdbcTemplate.queryForList(sql1.toString());
         
       JSONArray array = new JSONArray();
         JSONObject obj = new JSONObject();
         if(list !=null && list.size()>0){
             JSONObject jo = new JSONObject();
             for (Map<String, Object> map : list){
                 jo.put("parentUserCode", map.get("parentUserCode"));
                jo.put("siteName", map.get("siteName"));
                jo.put("expressNO", map.get("expressNO"));
                jo.put("takeDate", map.get("takeDate"));
                jo.put("enterDate", map.get("enterDate"));
               array.add(jo);
             }
             
             obj.put("rows", array);
         }else{
             
             obj.put("rows", null);
         }
         return obj;
   }
/**
    * 導出未打印訂單
    * @param req
    * @param response
    * @return
    */
   public String exportExcleO(HttpServletRequest req,HttpServletResponse response,String startDate,String endDate){
      startDate = req.getParameter("startDate");
         endDate = req.getParameter("endDate");
        JSONObject jso = queryOrder(req, startDate, endDate);
                 JSONArray json = jso.getJSONArray("rows");
                 
                 if (json.size()<0 && json==null) {
                     return null;
                 } else {
                     HSSFWorkbook wb = new HSSFWorkbook();
                     HSSFSheet sheet = wb.createSheet("表1");
                     HSSFRow row = sheet.createRow((int) 0);
                     HSSFCellStyle style = wb.createCellStyle();
                     style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 建立一個居中格式

                     HSSFCell cell = row.createCell((short) 0);
                     cell.setCellValue("站點編號");
                     cell.setCellStyle(style);
                     cell = row.createCell((short) 1);
                     cell.setCellValue("站點名稱");
                     cell.setCellStyle(style);
                     cell = row.createCell((short) 2);
                     cell.setCellValue("接單時間");
                     cell.setCellStyle(style);
                     cell = row.createCell((short) 3);
                     cell.setCellValue("下單時間");
                     cell.setCellStyle(style);
                    for (int i = 0; i < json.size(); i++) {
                         JSONObject f = json.getJSONObject(i);
                         row = sheet.createRow((int) i + 1);
                         row.createCell((short) 0).setCellValue(f.getString("parentUserCode"));
                         sheet.setColumnWidth(0, 20 * 200);
                         row.createCell((short) 1).setCellValue(f.getString("siteName"));
                         sheet.setColumnWidth(1, 20 * 300);
                         row.createCell((short) 2).setCellValue(f.getString("takeDate"));
                         sheet.setColumnWidth(2, 20 * 300);
                         row.createCell((short) 3).setCellValue(f.getString("enterDate"));
                         sheet.setColumnWidth(3, 20 * 300);
                        }
                     response.reset();
                     response.setContentType("application/x-msdownload");
                     String pName = "未打印訂單記錄";
                     try {
                         response.setHeader("Content-Disposition", "attachment; filename="
                                 + new String(pName.getBytes("gb2312"), "ISO-8859-1")
                                 + ".xls");
                     } catch (UnsupportedEncodingException e1) {
                         // TODO Auto-generated catch block
                         e1.printStackTrace();
                     }
                     ServletOutputStream outStream = null;

                     try {
                         outStream = response.getOutputStream();
                         wb.write(outStream);
                     } catch (Exception e) {
                         e.printStackTrace();
                     } finally {
                         try {
                             outStream.close();
                         } catch (IOException e) {
                             e.printStackTrace();
                         }
                     }
                     return pName;
                     }
     }
   
/***
     * 導出未打印訂單
     */
    @RequestMapping(value = "/Orderdownload", method = { RequestMethod.POST,
            RequestMethod.GET })
    @ResponseBody
    public void exportOrder(
            @RequestParam("startDate") String startDate,
            @RequestParam("endDate") String endDate,HttpServletResponse resp,HttpServletRequest req) {
        
        
        nceServiceImpl.exportExcleO(req, resp, startDate, endDate);
    }
相關文章
相關標籤/搜索