使用filesaver保存后无法打开excelfile

问题描述

在后端中,我生成一个Excel文件并将其保存在服务器上。很好但是用户必须有机会将excel工作表保存在本地计算机上。这就是为什么我在前端使用FileSaver的原因。

当前端获取字节数组时,它将保存一个文件。但是,当我在Excel中打开此文件时,收到消息,提示格式/扩展名无效。我在做什么错了?

后端:

[HttpGet]
public async Task<IActionResult> GetExcelAsync(){
        // do something in service layer
        var result = File.ReadAllBytes(excelFile);
        return Ok(result);
}

前端:

service.ts

public getExcelSheet(): Observable<any>{
        let options = {
        headers: new HttpHeaders({
                'Content-Type': 'application/json'}),responseType:  'blob' as 'json'};
        return this.httpClient.get<any>(this.url,options);
} 

component.ts

public getSheet(){
        this.service.getExcelSheet().subscribe( response =>{
        const blob = new Blob([response],{type: 'application/vnd.ms-excel'});
        FileSaver.saveAs(blob,'result.xlsx');})
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)