导出Excel功能-从服务端到浏览器的简单处理

28 篇文章 0 订阅
8 篇文章 0 订阅

导出Excel功能

从服务端到浏览器的简单处理, 仅供参考

服务端定义一个导出功能的关键代码

Java 定义一个export的功能函数,以下为关键代码(接口中的一部分处理逻辑):

@Override
public byte[] export(String startdate, String enddate, Integer type, String name,
 HttpServletResponse response) {
    // other logic omitted here ...

    // set response args,it used for open download
    response.reset();
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
    try {
        response.setHeader("Content-Disposition", "attachment;filename=" + 
        new String((name+ ".xls").getBytes(), "iso-8859-1"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        workList.write(output); // write
    } catch (IOException e) {
        e.printStackTrace();
    }
    return output.toByteArray();
}

前端处理

  1. 方式1:使用ng1实现,在自定义指令中来通过接口接收数据流 [ 存在兼容问题的方式 ]

指令如下:

angular.module('yourNgApp')
    .directive('sysExport', function ($http, $document) {
        return {
            templateUrl: "app/system/export/export.directive.html",
            restrict: 'EA',
            replace: true,
            scope: {
                item: '='
            },
            link: function (scope, element) {
                // 访问请求的函数
                function getExport(postData) {
                    // 拼出自己的url
                    var url = '/api/course/export?startdate=' + 
                    postData.startdate + '&enddate=' + postData.enddate + 
                    '&type=' + postData.type + '&name=' + postData.name;
                    // 注意下面的responseType 一定要有哦!
                    var finalPostData = {
                        url: url,
                        method: 'POST',
                        responseType: "arraybuffer",
                        headers: {'Content-type': 'application/json'}
                    };
                    $http(finalPostData)
                        .success(function (data) {
                            var blob, fileName, blobArgs;
                            blobArgs = {type: 'application/vnd.ms-excel application/x-excel'};
                            blob = new Blob([data], blobArgs);
                            var a = $document[0].createElement('a');
                            a.download = postData.name + '.xls';
                            a.href = URL.createObjectURL(blob);
                            a.click();
                        })
                        .error(function (e) {
                            console.log(e);
                        });
                }

                element.on('click', function () {
                    getExport(scope.item);
                });
            }
        };
    });
  • 至此可实现通过指令的点击事件来下载excel格式了。
  • 因为导出功能多用于后台管理,适用性不是很普遍,所以兼容问题可协商,所以前台使用了Blob对象。
  • 为了节约服务器资源可通过此种方式通过点击来下载,而不是将导出的文件单独存放到服务器上通过返回excel所在的url地址来进行下载。

2)方式2:直接通过a链接访问,[兼容性好]

示例:

<a href="/api/course/export?startdate={{cl.startdate}}&enddate={{cl.enddate}}&type={{cl.type}}&name={{cl.name}}" class="btn btn-success">导出</a>

扩展

总结

上面前端用到的都是一些H5高级API 适用性不是很高, 但也临时解决了一些问题,只作为尝试 :)
通过a链接的方式请求, 直接在服务器端完成文件的操作,点击后直接下载,兼容良好,但不适合大文件的传输下载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wang's Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值