Java poi生成 Excel

11 篇文章 0 订阅
2 篇文章 0 订阅
    /**
     * 文件导出
     * 
     * @param request
     * @param response
     * @throws IOException
     */
    public void exportData2(HttpServletRequest req,HttpServletResponse res) throws IOException{
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String yjjb = req.getParameter("yjjb");
        String cdbh = req.getParameter("cdbh");
        List<DynaBean> dataDybList = dao.queryDataListAll(startTime, endTime, cdbh, yjjb);
        JSONArray dataList = JSONArray.fromObject(dataDybList);
        String sysFolder = "";
        List<SystemCode> codeList = SystemCodesCache.getInstance().getSystemCodeList("XNML");
        if (!BeanUtils.emptyCollection(codeList)) {
            for (SystemCode code : codeList) {
                String bm = code.getCodeId();
                if(bm.equals("ROOT")){
                    sysFolder = code.getValue();
                    break;
                }
            }
        }
        String saveFilePath = sysFolder +"/SandWeather";
        String saveName = "XJSandWeather"+DateUtils.format(new Date(), "yyyyMMddHH")+".xls";
        
        List<String> headerEn = Arrays.asList("CSBH","CSMC","STARTTIME","ENDTIME","SJLX","MINAQI","MAXAQI","CXSJ","YJXQ","YBCSRQ");//字段
        List<String> headerZh = Arrays.asList("城市编号","城市名称","开始时间","结束时间","预报模式","最小值","最大值","持续时间","预警等级","预警批次");//字段名称
        
        File fPath = new File(saveFilePath);
        if(!fPath.exists()){
            fPath.mkdirs();
        }
        String absPath = saveFilePath+File.separator+saveName;
        if(JXLExcelUtil.WriteDataToExcel(absPath, headerZh, headerEn, dataList)){
                try{
                    String fileName = "文件名.xls";    //输出页面文件名称
                    // 设置输出的格式
                    res.reset();
                    res.setContentType("bin");
                    res.addHeader("Content-Disposition", "attachment; filename=\"" + new String((fileName).getBytes(),"ISO-8859-1") + "\"");
                    // 循环取出流中的数据
                    InputStream inStream = new FileInputStream(absPath);
                    byte[] b = new byte[100];
                    int len;
                    
                    try {
                        while ((len = inStream.read(b)) > 0)
                            res.getOutputStream().write(b, 0, len);
                        inStream.close();
                    }catch (IOException e2) {
                        logger.error("下载文件失败!" + e2.getMessage());
                        e2.printStackTrace();
                    }
                }catch(Exception e){
                    logger.error("下载文件失败!" + e.getMessage());
                    e.printStackTrace();
                }
            }
    }

以流的形式传到前台    ;

 接收的参数,和查询的数据,文件名    等自行修改   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值