把数据导出成Excel表格

具体原理不了解

HTML

JS

exportFactoryElectricInfo:function(){
            var type=$('#type').val();
            var townId = $('#townId').val();
            var tradeId = $('#tradeId').val();
            var startTime=$('#startTime').val();
            var endTime=$('#endTime').val();
            var url=__ctx+"/platform/common/export/exportFactoryElectricInfo?type="+type+"&tradeId="+tradeId+"&startTime="+startTime+"&endTime="+endTime+"&townId="+townId;
            window.location.href=url;
        },

Controller

 @RequestMapping("exportFactoryElectricInfo")
    public  void exportFactoryElectricInfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Long userId = ContextUtil.getLoginUserId();
        int  value= RequestUtil.getInt(request, "type",1);
        Date startTime = null;
        Date endTime = null;
        if(value == 1){
            endTime = new Date();
            startTime = DateUtil.getCurrentDayStart();
        }else if(value==2){
            endTime = new Date();
            startTime = DateUtil.getCurrentMonthStart();
        }else{
            startTime=RequestUtil.getDate(request, "startTime","yyyy-MM-dd HH:mm:ss");
            endTime=RequestUtil.getDate(request, "endTime","yyyy-MM-dd HH:mm:ss");
        }
        Long townId = RequestUtil.getLong(request, "townId",0L);
        Long tradeId=RequestUtil.getLong(request,"tradeId");
        List factoryList=factoryService.getListByTradeTownIdAndUserId(userId,tradeId,townId);
        List facEleList = electricHourService.getFactoryElectricInfo(factoryList,startTime,endTime);
        SimpleDateFormat dt= new SimpleDateFormat("yyyy.MM.dd");
        String start=dt.format(startTime);
        String end=dt.format(endTime);
        ArrayList keysArray = new ArrayList<>();
        ArrayList columnNamesArray = new ArrayList<>();
        columnNamesArray.add("企业名称");
        keysArray.add("name");
        columnNamesArray.add("行业名称");
        keysArray.add("industryName");
        columnNamesArray.add("乡镇名称");
        keysArray.add("townName");
        columnNamesArray.add("用电量(kwh)");
        keysArray.add("totalPower");
        String fileName="企业用电量";
        Boolean flag= request.getHeader("User-Agent").indexOf("like Gecko")>0;
        if (request.getHeader("User-Agent").toLowerCase().indexOf("msie") >0||flag){
            fileName = URLEncoder.encode(fileName, "UTF-8");//IE浏览器
        }
        String json = JacksonUtil.toJsonString(facEleList);
        List> list = ExcelUtil.createExcelRecord(json, keysArray, "null");
        ExcelUtil.exportDevData(response, list, keysArray, columnNamesArray, fileName);
    }

工具类

 public static void exportDevData(HttpServletResponse response, List> list, ArrayList keysArray, ArrayList columnNamesArray, String factoryName) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            createDevWorkBookData(list, keysArray, columnNamesArray, factoryName).write(os);
        } catch (IOException var21) {
            var21.printStackTrace();
        }
        byte[] content = os.toByteArray();
        InputStream is = new ByteArrayInputStream(content);
        response.reset();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String((factoryName + ".xlsx").getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (IOException var22) {
            throw var22;
        } finally {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
        }
    }

 

你可能感兴趣的:(javaweb程序设计)