简体   繁体   English

使用Aspose.Cells和Angular 2前端从asp.net Web API后端保存Excel电子表格时遇到问题

[英]Having trouble saving Excel spreadsheet from asp.net web api backend using Aspose.Cells, and Angular 2 front end

On my ASP.NET web api backend I have Aspose.Cells make a spreadsheet. 在我的ASP.NET Web API后端上,我有Aspose.Cells制作电子表格。 I have been saving it locally and the spreadsheet is good, but now that I am trying to host it on a server I have to send the spreadsheet through the HTTP response. 我已经将其保存在本地,并且电子表格很好,但是现在我试图将其托管在服务器上,因此我必须通过HTTP响应发送电子表格。 At the end of my web api route I have the following code: 在我的Web API路线的末尾,我有以下代码:

workbook.Save(HttpContext.Current.Response, output.xls, ContentDisposition.Inline, new XlsSaveOptions());
HttpContext.Current.Response.End();

This sends the response which can be seen here in Chrome dev tools: 这会发送响应,您可以在Chrome开发工具中看到此响应: 在此处输入图片说明

As you can see, the body is just random gibberish mostly which I'm assuming is just binary. 如您所见,主体只是乱码,我假设只是二进制。 On the client my typescript inside the subscribe method which receives the response looks like this: 在客户端上,我的订阅方法内部的打字稿接收响应,如下所示:

var blob = new Blob([data._body], { type: 'application/vnd.ms-excel' });
        var url = window.URL.createObjectURL(blob);
        window.open(url);

This saves the file with a name like f811c3bc-2eea-4394-b463-93c61ccc7677.xls, and inside the spreadsheet it again just looks like gibberish, after excel warns me that the file is a different format than the extension: 这样会保存文件,其名称类似于f811c3bc-2eea-4394-b463-93c61ccc7677.xls,而在excel警告我该文件的格式与扩展名不同之后,电子表格中的文件看上去又像是乱七八糟的:
在此处输入图片说明

The words 'detail' and 'site detail' are words that should be in there, so at least something that I want shows up, but its obviously not quite there. “详细信息”和“现场细节”这两个词应该放在其中,因此至少我想要显示一些内容,但显然不完全是。 What am I doing wrong? 我究竟做错了什么? This is supposed to be a spreadsheet with 20+ tabs and up to a couple thousand rows on a few of the tabs, and less than 10 on others. 这应该是一个包含20个以上标签的电子表格,其中几个标签最多可包含数千行,而其他标签则少于十个。

EDIT: I have fixed my problem for anyone searching from the future. 编辑:我已经解决了以后搜索任何人的问题。 On your HTTP request options, you need to set the response type to ArrayBuffer. 在HTTP请求选项上,您需要将响应类型设置为ArrayBuffer。 I also switched to using Filesaver.js although I'm not sure if that is actually needed or not. 我也切换到使用Filesaver.js,尽管不确定是否确实需要这样做。

We are afraid, Web Browsers don't understand XLS (Excel 2003) and XLSX (Excel 2007 and newer) format. 恐怕Web浏览器无法理解XLS(Excel 2003)和XLSX(Excel 2007及更高版本)格式。 Only Microsoft Excel understands it. 只有Microsoft Excel可以理解。 It means, you cannot show your Excel files in Web Browsers. 这意味着您无法在Web浏览器中显示Excel文件。 You can just ask the user to download them and then users will have to open them in their Microsoft Excel application on their own. 您可以只要求用户下载它们,然后用户必须自己在Microsoft Excel应用程序中打开它们。

However, some of the browsers understands (or they have plug-ins) Pdf format . 但是,某些浏览器理解(或具有插件) Pdf格式 If you want to show your Excel file contents, then render it to Pdf using Aspose.Cells and then user will be able to view it inline if he is using Google Chrome, FireFox etc. 如果要显示Excel文件的内容,请使用Aspose.Cells将其渲染为Pdf,然后,如果用户使用的是Google Chrome,FireFox等,则可以内联查看它。

Anyhow, you will have to send it as attachment. 无论如何,您必须将其作为附件发送。 Please change your code like this. 请像这样更改您的代码。

workbook.Save(HttpContext.Current.Response, output.xls,ContentDisposition.Attachment, new XlsSaveOptions());
HttpContext.Current.Response.End();

Note: I am working as Developer Evangelist at Aspose 注意: 我在Aspose担任开发人员布道者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.net WebApi Aspose.Cells - 导出和下载 excel - Asp.net WebApi Aspose.Cells - export and download excel 将ASP.NET MVC或Web API 2后端+ Angular用于仪表板平台 - Using ASP.NET MVC or Web API 2 backend + Angular for dashboard platform 如何从ASP.NET中的前端Javascript函数调用后端C#函数 - How to call a backend C# function from front end Javascript function in ASP.NET 如何通过url查询字符串在前端保存承载令牌? ASP.NET中的RESTful API - How to save a bearer token in front end from a url query string? RESTful api in ASP.NET 使用 Axios post 方法从 Asp.Net Web API 下载 Excel - Excel download from Asp.Net Web API using Axios post method 使用 HTTP Post(RESTful API)将来自 React.JS 前端的请求传递到 C# 后端(ASP.NET) - Passing a Request from React.JS front-end to C# back-end (ASP.NET) using HTTP Post (RESTful API) 遇到麻烦的ASP.NET Web API 2 - ASP.NET Web API 2 with Angular woes 有关基于ASP.NET MVC2的富客户端Web应用程序的前端实现的建议 - Advice on front end implementation of rich client web app based on ASP.NET MVC2 如何使用 knockout.js 从 front-end.js 文件中检索 asp.net appSettings 键 - howto retrieve asp.net appSettings key from front-end .js file using knockout.js 将Excel文件发送到ASP.NET Web API - Send Excel File to ASP.NET Web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM