Free Support Forum - aspose.com

Write excel file using strategy design pattern

Hi,

We are building an application where in user will be able to export a list containing data using aspose.cells .NET.

What I have achieved so far is that, I have been able to get the data, transform, filter and write it physically to an excel file (thanks to easily consumable aspose libraries.

Our application’s design is as follows :

  1. An XHR request hits the RESTful service (.NET WebAPI 2).
  2. The service calls a business component which first gathers data from database and then passes it to a library that performs the job of transforming the data and filtering it.
  3. The result data is then sent to another library which implements Strategy pattern as follows:
    ----An object is instantiated based on what type of export is expected viz. Excel or Pdf
    ----The result set is passed to an export method of the instantiated object that will generate excel or pdf and return a response.

What I need to do is :
I want the method that generates the excel and pdf file to return HttpResponseMessage that I can blindly return to the client and then the client can interpret the response type and download a file physically.

Also, to add, I tried certain ways to do it, my browser keeps giving me CORS error. I also tried adding allow-access-origin for content disposition to header, but no luck,

Please help! Any guidance will be greatly appreciated. I have lost my depth here.

Cheers,
Yogesh

@yogeshhindoliya,

Thanks for your query.

Apparently this query does not seem to be related to Aspose.Cells as it does not provide any feature to convert the resultant Excel or PDF file to HttpResponseMessage. However if we google we can have lot of solutions as given below:

[HttpGet]
public HttpResponseMessage Generate()
{
	Workbook wb = new Workbook(FileFormatType.Xlsx);
	string[,] array = new string[,]
	{
		{"cat", "dog"},
		{"bird", "fish"},
	};
	wb.Worksheets[0].Cells.ImportTwoDimensionArray(array, 0, 0);
	MemoryStream stream = wb.SaveToStream();

    var result = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new ByteArrayContent(stream.ToArray())
    };
    result.Content.Headers.ContentDisposition =
        new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
    {
        FileName = "output.xlsx"
    };
    result.Content.Headers.ContentType =
        new MediaTypeHeaderValue("application/octet-stream");

    return result;
}

You may also visit following for your reference.
Returning binary file from controller in ASP.NET Web API

I tried this approach. Unfortunately, it doesn’t fit my requirements.

Apologies, I should have been more clear. All I need is to download an excel or a pdf file without actually needing to write it physically. The implementation should follow the design mentioned in the topic.

Cheers,
Y

@yogeshhindoliya,

You can save a file to a stream with Aspose.Cells API and then devise a method to work with HttpResponseMessage or HttpResponse object. See the document with example code for your reference:

https://docs.aspose.com/display/cellsnet/Saving+File+to+Response+Object
https://docs.aspose.com/display/cellsnet/Different+Ways+to+Save+Files#DifferentWaystoSaveFiles-SavingFiletoaStream

Let us know if you encounter any issue with Aspose.Cells API, we will be glad to look into it and help you further.

I tried the approach above, I am able to get “some” response at the client side unfortunately, what I am unable to decide is how to convert the stream data to an excel file.

I understand this is a bit irritating and unrelated to Aspose.cells or for that matter aspose at all, but I am a noob here looking to learn.

Attached is a screenshot of the file that downloads. I can see some data (atleast the text "created with Aspose.Cells for .NET…) but rest all the data is gibberish. What could be the problem?

Please advise.

Cheers,
Y

Country_Gibberish.png (27.9 KB)

@yogeshhindoliya,

You are seeing the text “Created with Aspose.Cells for .NET…” because of evaluation limitation as mentioned in below help topic.
https://docs.aspose.com/display/cellsnet/Licensing

Moreover, I am afraid these forums are specific to Aspose APIs, so we cannot help you much about manipulation of streams. Please browse the internet and you will certainly learn about your requirements. If you encounter the issue while saving the file to stream in your local environment, then share your sample application and we will investigate if Aspose.Cells API is causing the problem.

Thanks, @ahsaniqbalsidiqui.

I debugged and wrote intermediate file on the server side and found that there was no problem with the file being generated.
It’s my client code that’s causing the problems.
I believe we can close this thread now.

Cheers,
Yogesh

@yogeshhindoliya,

Thank you for the feedback.

Good to know that file is generated fine on your end. Should you have any query or issue with Aspose.Cells, feel free to contact us, we will be happy to assist you soon.

1 Like