2

I am using Web API 4.5.1. I am generating API in C# code and passing this API to front end, where the front end is built on HTML and AngularJS JavaScript. My requirement is to create an API for export to Excel to the front end. I am a kind of stuck here. I am receiving request from front end people with fromdate and todate. Based on that I will query the database to get the data and if I am successful, get the HTML format out of it. I am unable to go further as I am new to this. Below is the code which I am trying to achieve this with:

[HttpPost]
public HttpResponseMessage ExportToExcel(string fromTimeStamp, string toTimeStamp)
{
    DateTime fromDate = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
    fromDate = fromDate.AddMilliseconds(Convert.ToDouble(fromTimeStamp)).ToLocalTime();
    fromDate.AddDays(1);

    DateTime toDate = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
    toDate = toDate.AddMilliseconds(Convert.ToDouble(toTimeStamp)).ToLocalTime();
    toDate.AddDays(1);

    var fromDate1 = fromDate.AddDays(1).ToString("yyyy-MM-dd");
    var toDate1 = toDate.AddDays(1).ToString("yyyy-MM-dd");

    var bal = new AdminBAL();
    var ExportData = bal.ExportToExcel(fromDate1, toDate1);
    //where Exportdata is the html format for the data required.
    // unable to find the solution here
}

How can I complete this operation?

2
  • There are plenty of questions on SO which help you to write an excel file, and others which explain how to send an excel file as a download via Web API. Look for those and try to combine them, but don't expect us to write the code for you. Commented Jun 1, 2015 at 9:54
  • 1
    A simple excel library can be found here: epplus.codeplex.com Commented Jun 1, 2015 at 9:55

1 Answer 1

6

Can you try this?

 var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
        //If you have Physical file Read the fileStream and use it.
        response.Content = new StreamContent(fileStream);
        //OR
        //Create a file on the fly and get file data as a byte array and send back to client
        response.Content = new ByteArrayContent(fileBytes);//Use your byte array
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = fileName;//your file Name- text.xls
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/ms-excel");
        //response.Content.Headers.ContentType  = new MediaTypeHeaderValue("application/octet-stream");
        response.Content.Headers.ContentLength = fileStream.Length;
        response.StatusCode = System.Net.HttpStatusCode.OK;

I got this works fine.Please let me know if you have any doubts on this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.