在Angularjs和WebApi中下载Excel文件xlsx [英] Downloading Excel file xlsx in Angularjs and WebApi

查看:475
本文介绍了在Angularjs和WebApi中下载Excel文件xlsx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个任务,我必须以xlsx格式下载报告。报表文件从服务器成功生成,并在客户端接收。但是它不会打开并产生无效的格式错误。

以下是服务器端的代码。


I am working on a task, in which I have to download a report in xlsx format. The report file is generated successfully from server, and is received on client side as well. But it is not opening and producing invalid format error.

Below is the code of server side.

var output = await reportObj.GetExcelData(rParams);
    if (output != null){
        var result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(output.ConentBytes)
        };
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = output.FileName
        };
 return result;
 }


这是客户端的代码:


Here is the code for client side:

        var saveData = function (response) {

        if (response.status === 200) {
            var reportData = response.data;

            var b = new Blob([reportData], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
            saveAs(b,"ReportFile.xlsx");//this is FileSaver.js function
        } else {
            console.log(response.statusText);
        }

    };


    $scope.getExcelFile = function(reportName, reportParams) {

        reportDataService.getExcelReportData(reportName, reportParams, saveData);

    }


以下是错误信息:

由于某些内容不可读,Excel无法打开newFile.xlsx。您要打开并修复此工作簿吗?


点击修复,出现以下错误:
Excel无法打开此文件。 >
文件格式或文件扩展名无效。验证文件是否已损坏,并且文件扩展名与文件的格式相符。

可以有人指导我做错了什么?同时,同一个服务器端的文件生成器对象在ASP.Net表单应用程序中运行顺畅,文件打开也没有任何错误。
ACK谢谢。


Below is the error message:
Excel could not open newFile.xlsx because some content is unreadable. Do you want to open and repair this workbook?

On clicking repair, following error appears: Excel cannot open this file.
The file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

Can some one guide me what I am doing wrong? Meanwhile, the same server side file generator objects works smoothly in ASP.Net forms application, and the file opens without any error as well.
Thank you.

推荐答案

我希望你的 $ http 调用缺少响应类型配置。这是我下载办公文件的方式:

I expect your $http call is missing the response type configuration. This is the way I download office files:

function download(url, defaultFileName) {
    var self = this;
    var deferred = $q.defer();
    $http.get(url, { responseType: "arraybuffer" }).then(
        function (data, status, headers) {
            var type = headers('Content-Type');
            var disposition = headers('Content-Disposition');
            if (disposition) {
                var match = disposition.match(/.*filename=\"?([^;\"]+)\"?.*/);
                if (match[1])
                    defaultFileName = match[1];
            }
            defaultFileName = defaultFileName.replace(/[<>:"\/\\|?*]+/g, '_');
            var blob = new Blob([data], { type: type });
            saveAs(blob, defaultFileName);
            deferred.resolve(defaultFileName);                    
        }, function (data, status) {
            var e = /* error */
            deferred.reject(e);
        });
    return deferred.promise;
}

这篇关于在Angularjs和WebApi中下载Excel文件xlsx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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