`

导出报表Content-Disposition

 
阅读更多

论坛有个需求,导出参加活动的人数情况,用报表来保存,详细代码如下:

@RequestMapping(value = "/activity/export")
    @ResponseBody
    public String export(HttpServletRequest request, HttpServletResponse response) {

        long tid = NumberUtils.toLong(request.getParameter("tid"), 0l);

        Topic topic = topicService.findTopic(tid);

        Activity activity = activityService.findActivity(tid);

        response.reset();//清空缓存
        response.setCharacterEncoding("GBK");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + "activity_"
                + topic.getTid() + ".csv\"");//生成的文件初始文件名称

        //获取报表内容
        StringBuilder sb = activityService.getExportContent(activity, topic);

        OutputStream os = null;
        try {
            os = response.getOutputStream();
            byte[] byt = sb.toString().getBytes();
            os.write(byt);
        } catch (Exception e) {
            log.error("输出活动参与报表失败", e.getStackTrace());
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                log.error("输出活动参与报表关闭输出流失败", e.getStackTrace());
            }
        }

        return null;
    }

 其中activityService.getExportContent(activity, topic)这个方法是报表内容,关键代码如下:

//参与活动用户信息
        boolean username = false, persons = false, phone = false, city = false,
                carNum = false, email = false, postNum = false, address = false, desc = false;
        sb.append("昵称");
        if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_USERNAME))) {
            sb.append(",");
            sb.append("真实姓名");
            sb.append(",");
            sb.append("用户ID");
            sb.append(",");
            sb.append("用户名");
            username = true;
        }
        if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_PERSONS))) {
            sb.append(",");
            sb.append("参加人数");
            persons = true;
        }
        if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_PHONE))) {
            sb.append(",");
            sb.append("联系电话");
            phone = true;
        }
        if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_CITY))) {
            sb.append(",");
            sb.append("所在城市");
            city = true;
        }
        if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_CARNUM))) {
            sb.append(",");
            sb.append("车牌号码");
            carNum = true;
        }
        if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_EMAIL))) {
            sb.append(",");
            sb.append("电子邮件");
            email = true;
        }
        if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_POSTNUM))) {
            sb.append(",");
            sb.append("邮政编码");
            postNum = true;
        }
        if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_ADDRESS))) {
            sb.append(",");
            sb.append("联系地址");
            address = true;
        }
        if (Boolean.parseBoolean(appConfigService.getValueByName(SystemConstant.ACTIVITYITEM_CONFIG_SHOW_DESC))) {
            sb.append(",");
            sb.append("特别说明");
            desc = true;
        }
        sb.append(",报名时间,状态");
        sb.append(separator);

        User regUser = null;
        List<ActivityItem> list = activityItemService.queryActivityItem(activity.getTid(), null, true);
        for (ActivityItem item : list) {
            sb.append(item.getAuthor().getNickname()).append(",");
            if (username) {
                sb.append(item.getName()).append(",");
                sb.append(item.getUid()).append(",");
                regUser = userService.findUser(item.getUid());
                if (regUser != null) {
                	sb.append(regUser.getUsername()).append(",");
                } else {
                	sb.append("").append(",");
                }
            }
            if (persons) {
                sb.append(item.getPersons()).append(",");
            }
            if (phone) {
                sb.append("\t" + item.getPhone() + "\t").append(",");
            }
            if (city) {
                sb.append(item.getCity()).append(",");
            }
            if (carNum) {
                sb.append(item.getCarNum()).append(",");
            }
            if (email) {
                sb.append(item.getEmail()).append(",");
            }
            if (postNum) {
                sb.append(item.getPostNum()).append(",");
            }
            if (address) {
                sb.append(item.getAddress()).append(",");
            }
            if (desc) {
                sb.append(item.getDescription().replace("\r","").replace("\n","").replaceAll("\"","”").replaceAll(",", " ")).append(",");
            }
            sb.append(sdf1.format(item.getCreateAt())).append(",")
                .append(item.getStatus() == SystemConstant.ACTIVITY_ITEM_NOT_CENSOR ? "尚未审核"
                        : item.getStatus() == SystemConstant.ACTIVITY_ITEM_CENSOR_PASS ?
                        "审核通过" : "被拒绝").append(separator);

 

导出的文件内容:

 

关于上面的方法有几个说明:

1.后缀为csv的文件,可以用excel打开,来自百科的解析:逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。通常,所有记录都有完全相同的字段序列。

2.Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。当 Internet Explorer 接收到头时,它会激活文件下载对话框,它的文件名框自动填充了头中指定的文件名。(请注意,这是设计导致的;无法使用此功能将文档保存到用户的计算机上,而不向用户询问保存位置。)  

 

 

  • 大小: 10.9 KB
分享到:
评论

相关推荐

    content-disposition:创建并解析HTTP Content-Disposition标头

    内容倾向 创建并解析HTTP Content-Disposition标头安装$ npm install content-dispositionAPI var contentDisposition = require ( 'content-disposition' )contentDisposition(文件名,选项) 使用给定的文件名...

    content-disposition-parser:解析内容处置HTTP标头

    $ npm install content-disposition-parser 用法 const parser = require ( 'content-disposition-parser' ) parser ( 'content-disposition: inline; filename=example.pdf' ) //-&gt; { filename: 'example.pdf', ...

    header中Content-Disposition的作用与使用方法

    本文章详细的介绍了关于php header中Content-disposition用法详细,有需要了解header用法的朋友可参考一下

    将数据导出到Excel

    将数据导出到Excel源代码及方法:response.setContentType("application/vnd.ms-... response.addHeader("Content-Disposition", "attachment;filename=logininfo.xls"); String name = request.getParameter("name");

    Content-Disposition使用方法和注意事项

    最近不少Web技术圈内的朋友在讨论协议方面的事情,有的说web开发者应该熟悉web相关的协议,有的则说不用很了解。...本文我们来说一下MIME 协议的一个扩展Content-disposition。 我们在开发web系统时有时会有以下需求

    bug-chromium-pdf-content-disposition-encoding

    bug-chromium-pdf-content-disposition-encoding

    http/formdata

    Content-Disposition: form-data; name=“projectName” testProject ----------------------------904587217962624105581666 Content-Disposition: form-data; name=“clientName” aaa ------------------------...

    PHP附件下载中文名称乱码的解决方法

    分享给大家供大家参考,具体如下: ... 此时就需要对标题进行编码,也就是说先进性urlencode,然后再放入...网上说,在RFC2231的定义里面, 多语言编码的Content-Disposition应该这么定义: 复制代码 代码如下:Content-

    2021科教版4下测试卷.pdf

    2021科教版4下测试卷.pdf

    会员数据导出插件 for 帝国CMS.rar

     33行所导出的文件名 header("Content-Disposition:filename=1.xls"); excel_enewsmemberadd.php文件中, 修改25行表的名称 $DB_TBLName = "xj_enewsmemberadd";  33行所导出的文件名 header("Content-...

    cgi实现下载文件

    使用cgi代码在网页上实现文件的下载功能。

    ( response.setHeader()下载中文文件名乱码

    ( response.setHeader()下载中文文件名乱码问题

    下载文件个别浏览器文件名乱码解决办法

    代码如下: if (context.Request.UserAgent.ToLower().IndexOf(“msie”, System.StringComparison.Ordinal) &gt; -1)//IE浏览器 { context.Response.AddHeader(“content-disposition”, “filename=” + ...

    Struts2实现的多文件上传例子

    NULL 博文链接:https://hdxiong.iteye.com/blog/338026

    nginx文件强制下载的配置方法

    添加头信息Content-Disposition “attachment;”会使浏览器强制下载: 代码如下: #表示浏览器内嵌显示一个文件 Content-disposition: inline; filename=foobar.pdf   #表示会下载文件,如火狐浏览器中 Content-...

    使用Springboot实现excel的导入导出

    设置响应头: 设置响应的Content-Type为application/vnd.ms-excel,并设置Content-Disposition为attachment; filename=employees.xlsx,这样浏览器就会将响应解释为要下载的Excel文件。 将Excel写入响应流: 使用...

    gradle-6.7-bin.zip

    gradle-6.7-bin.zip 分享了,官网站下载的下载地址: https://dl-download.csdn.net/down11/20190419/11e97afb8a4c05a6f253270eecc9f3a7.zip?response...

    Http协议header说明

    Http协议header说明,包括: Content-Length Content-Language Content-Disposition Content-Type Cache-Control .............. 等属性使用

    swing导出Excel

    response.setHeader("Content-disposition","attachment; filename=clerkAddress.xls"); WritableWorkbook workbook = Workbook.createWorkbook(os); WritableSheet sheet = workbook.createSheet("TestCreateExcel...

Global site tag (gtag.js) - Google Analytics