How serve a downloadable excel file created with OpenXml, in .NET Core
Asked Answered
S

1

8

enter image description hereI generated an excel file with OpenXml 2.7.2 in .NETCoreApp 1.1 and saved it into a project folder. Then in my function I read the bytes and try to return it as a File. I don't get an error, but in the response I just get back the binary. So it seems to work but doesn't get downloaded.

Here is my code:

[HttpGet]
[Route("export")]
public IActionResult Export()
    {
        return File(System.IO.File.ReadAllBytes(filePath),
        contentType: "application/octet-stream",
        fileDownloadName: "MySheet.xlsx");
        
    }

If anyone knows how to serve a downloadable excel file in .net core let me know. Any help is appreciated thanks!

UPDATE

I'm not sure the corruption is related to generating the excel file with OpenXml because even when I try to export an empty file that wasn't generated with OpenXml I get the same error message saying "The file is corrupt and cannot be opened". Its also not entirely related to Excel 2016 because I can open other excel files with it fine.

Swallowtailed answered 15/8, 2017 at 21:40 Comment(11)
The code you have should work. I am having trouble understanding what you problem is. The downloaded file will be delivered as binary? What is that image in the question suppose to represent and what tool is that being used? How is the action being called?Elroy
The image shows what I get back in the response of my http call, which is the excel binary. I don't want that. I want the file to save onto a pc/mac under the name MySheet.xlsx. The tool i used to see the response was Chromes Inspect tool. I'm calling the export route from my front end which is in angular 2.Swallowtailed
If you just enter the url in the browser does it prompt you to download the file, or at least start downloading it if the browser is configured to do so.Elroy
Inspect the raw response and you should see that all the information relating to the file would be included along with the content. The server side code is accurate. It looks like you need to work on how the client side handles the responseElroy
So I tested the route in Swagger Ui and it does download. The problem was that I had a proxy the points from one port to another so it wasn't downloading but when I dont use the proxy it works. The file comes back corrupted though so still need to do some tweaking.Swallowtailed
At least it is confirmed that the download is working. Review how the file is generated to address the file corruption.Elroy
Thanks! Think I got it from hereSwallowtailed
try application/vnd.ms-excel.12 or application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (official MIME types for .xlsx) instead of application/octet-streamLavalley
Thanks, but no luck using those types, still says its corrupt.Swallowtailed
@Swallowtailed I thought you figured this out already. Anyways...when the file is generated are you able to go to the saved location and open the file? if it is corrupted even then, then that means that there is an issue with the generation of the file.Elroy
The saved file is fine. When I tested it with a excel file that I didn't generate with OpenXml, it also says it can't open because its corrupted. So it has to be something else besides the way I'm generating the file.Swallowtailed
S
2

I found a work around, so that I don't have to rely on reading the file and serving it which seems to be my issue. Now what I do is i generate the excel and return the url of the excel file. Then in the success response I call window.open().

this.export().subscribe(
        data => window.open(urlToExcelFile, "_blank"),
        err => console.log(err)  
    );
Swallowtailed answered 22/8, 2017 at 23:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.