Click here to Skip to main content
16,005,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I want to export data into excel format from database in WEB API2.0
Posted

1 solution

can you try this way


C#
// GET api/values/
    public HttpResponseMessage Get()
    {

        //DataTable Dt= GetData();  Getdata from database in thr form d
        //Generate  html table sturcture
        string str = "<table><tr> <th>colume1</th></tr><tr> <td>abc</td> </tr></table>";
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new StringContent(str);
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); //attachment will force download
        result.Content.Headers.ContentDisposition.FileName = "data.xlsx";
        return result;
    }
 
Share this answer
 
v2
Comments
Member 10419297 2-Nov-15 1:00am    
where is response and message defined
Anil Sharma1983 2-Nov-15 2:54am    
I have update the example with full code above. Please check
Member 10419297 2-Nov-15 4:06am    
Thank you so much sir
Member 10419297 2-Nov-15 4:36am    
what will be the code in case of reverse proces
Anil Sharma1983 2-Nov-15 4:47am    
http://www.codeproject.com/Articles/806075/File-Upload-using-jQuery-AJAX-in-ASP-NET-Web-API

This link will help you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900