如何下载xls文件用角和Spring REST服务? [英] How to download xls file with Angular and Spring Rest Service?

查看:152
本文介绍了如何下载xls文件用角和Spring REST服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个棱角分明的工厂叫弹簧休息服务下载xls文件。

I created a factory in angular to call a spring rest service to download xls file.

这是我厂的:

angular.module('app')
.factory('SportsbookServiceCustom', function ($http) {
    return {
        exportToXLS: function () {
            return $http.get('api/sportsbooks/downloadXLS').then(function (response) {
                return response.data;
            });
        }
    };
});

我有这样的code为我的REST服务:

I have this code for my rest service:

@RequestMapping(value = "/sportsbooks/downloadXLS")
public void downloadXLS(HttpServletResponse response) {
    Pageable pageable = new PageRequest(0, 20, Direction.ASC, "id");
    Page<Sportsbook> page = sportsbookRepositoryCustom.findAll(pageable, null, null, null);
    List<Sportsbook> sportsbookList = page.getContent();

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Sample sheet");

    Map<String, Object[]> data = new HashMap<String, Object[]>();
    data.put("1", new Object[] { "Emp No.", "Name", "Salary" });
    data.put("2", new Object[] { 1d, "John", 1500000d });
    data.put("3", new Object[] { 2d, "Sam", 800000d });
    data.put("4", new Object[] { 3d, "Dean", 700000d });

    Set<String> keyset = data.keySet();
    int rownum = 0;
    for (String key : keyset) {
        Row row = sheet.createRow(rownum++);
        Object[] objArr = data.get(key);
        int cellnum = 0;
        for (Object obj : objArr) {
            Cell cell = row.createCell(cellnum++);
            if (obj instanceof Date)
                cell.setCellValue((Date) obj);
            else if (obj instanceof Boolean)
                cell.setCellValue((Boolean) obj);
            else if (obj instanceof String)
                cell.setCellValue((String) obj);
            else if (obj instanceof Double)
                cell.setCellValue((Double) obj);
        }
    }

    if (workbook != null) {
        // Writing file to outputstream
        try {
            InputStream fileInputStream = new ByteArrayInputStream(workbook.getBytes());
            OutputStream output = response.getOutputStream();
            response.reset();
            response.setContentLength((int) (workbook.getBytes().length));

            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=yourFileName.xls");

            IOUtils.copyLarge(fileInputStream, output);
            output.flush();
        }

        catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

在code运行没有错误,但下载的文件是行不通的。什么是错误?

The code runs without error but downloading file isn't working. What is error?

推荐答案

filesaver.js

从服务器端通过JSON(这里春季)

Pass the Json from your Server side (here Spring)

<div id="exportable">
    <table width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>DoB</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="item in items">
                <td>{{item.name}}</td>
                <td>{{item.email}}</td>
                <td>{{item.dob | date:'MM/dd/yy'}}</td>
            </tr>
        </tbody>
    </table>
</div>

在你的角度,你的控制器接收到来自服务器eg.below项目

in your angular controller you received items from server eg.below

$scope.items = [{
        name: "John Smith",
        email: "j.smith@example.com",
        dob: "1985-10-10"
    }, {
        name: "Jane Smith",
        email: "jane.smith@example.com",
        dob: "1988-12-22"
    }, {
        name: "Jan Smith",
        email: "jan.smith@example.com",
        dob: "2010-01-02"
    }, {
        name: "Jake Smith",
        email: "jake.smith@exmaple.com",
        dob: "2009-03-21"
    }, {
        name: "Josh Smith",
        email: "josh@example.com",
        dob: "2011-12-12"
    }, {
        name: "Jessie Smith",
        email: "jess@example.com",
        dob: "2004-10-12"
    }]
}

添加按钮导出

<button ng-click="exportData()"> Export </button>

定义功能控制器

$scope.exportData = function () {
        var blob = new Blob([document.getElementById('exportable').innerHTML], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
        });
        saveAs(blob, "Report.xls");
    };

演示

这篇关于如何下载xls文件用角和Spring REST服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆