Please consider using the overloaded version of Aspose.Cells for .NET that can accept objects of HttpResponse & ContentDisposition as demonstrated below. Moreover, please confirm that there is no problem in your logic to create/manipulate the spreadsheet by saving the Workbook directly on the disk. In case the problem persists, please provide us a sample standalone application for our review.
C#
//Creating an Workbook object
Workbook workbook = new Workbook(FileFormatType.Xlsx); //… Your code goes here
//Save in xlsx format and send the file to user so that he may open the file in //some application or save it to some location
workbook.Save(this.Response, “Report.xlsx”, ContentDisposition.Attachment, new OoxmlSaveOptions());
Response.End();
There is no issue saving file on the disk. I am creating a rest web api to download excel file..my controller file is inherited from ApiController class and I am not able to use httpResponse object directly in my controller..that's why I am not able to use suggested code..
To workaround I have written follwing code.. Please let me know is that is best way or not..
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(filePath, FileMode.Open); response.Content = new StreamContent(stream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Report.xlsx" };
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(filePath, FileMode.Open); response.Content = new StreamContent(stream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Report.xlsx" };
Thank you for elaborating the requirements. Please allow us some time to look further into this matter, and get back to you with updates in this regard.
1) In MVC applications this.Response is a HttpResponseBase type rather than a HttpResponse type so you can replace the this.Response with System.Web.HttpContext.Current.Response in my previously proposed solution.
2) I have attached a sample application using the System.Web.Mvc.FileStreamResult to send the spreadsheet on client machine.
3) If you wish to open an Excel file from the browser using a controller, please consider the following snippet.
C#
MemoryStream stream = new MemoryStream();
workbook.Save(stream, FileFormatType.Excel97To2003);
WriteStream(stream, resultFileName);
I am sorry to know that the none of the proposed solutions are worth trying. Please attach your demo application along with a project consuming the service so we could test all possibilities on our side before getting back to you.
By the way, you have mentioned sample project from point 2) only. Have you tried other options (1 & 3)?
We humbly request you to please create a new thread and provide detailed problem description along with sample application (and dependencies). We will thoroughly analyze your case and get back with updates.