Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre>
Error	CS1503	Argument 1: cannot convert from 'System.Collections.Generic.List<WebApi2.Controllers.Record>' to 'System.IO.Stream'	


What I have tried:

[HttpGet]
        [Route("ExcelDownload")]
        public HttpResponseMessage ExcelDownload()
        {                       
            var reportStream = RecordInfo();
            var result = Request.CreateResponse(HttpStatusCode.OK);
            result.Content = new StreamContent(reportStream); // Getting error here
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "Report.xlsx"
            };

            return result;
        }


        public List<Record> RecordInfo()
        {
            List<Record> recordobj = new List<Record>();
            recordobj.Add(new Record { FName = "Smith", LName = "Singh", Address = "Knpur" });
            recordobj.Add(new Record { FName = "John", LName = "Kumar", Address = "Lucknow" });
            recordobj.Add(new Record { FName = "Vikram", LName = "Kapoor", Address = "Delhi" });
            recordobj.Add(new Record { FName = "Tanya", LName = "Shrma", Address = "Banaras" });
            recordobj.Add(new Record { FName = "Malini", LName = "Ahuja", Address = "Gujrat" });
            recordobj.Add(new Record { FName = "Varun", LName = "Katiyar", Address = "Rajasthan" });
            recordobj.Add(new Record { FName = "Arun  ", LName = "Singh", Address = "Jaipur" });
            recordobj.Add(new Record { FName = "Ram", LName = "Kapoor", Address = "Panjab" });
            recordobj.Add(new Record { FName = "Vishakha", LName = "Singh", Address = "Banglor" });
            recordobj.Add(new Record { FName = "Tarun", LName = "Singh", Address = "Kannauj" });
            recordobj.Add(new Record { FName = "Mayank", LName = "Dubey", Address = "Farrukhabad" });
            return recordobj;
        }
Posted
Updated 31-Oct-17 2:01am

1 solution

The error message is telling you exactly what the problem is, you cannot make a List<Record> into a stream object without going through a converter. And even if you could the output is not an Excel file.
 
Share this answer
 
Comments
[no name] 2-Nov-17 0:29am    
Ok. If you have can you please get me some working code which returns excel.xlsx file from web api.
Richard MacCutchan 2-Nov-17 4:41am    
Sorry, we are not here to do other people's work for them.
Raphael.UFRPE 16-Aug-21 10:10am    
speak for yourself, if I had the solution I would gladly give. this is not doing the work for him, this is helping him, we must help if this is within our reach, we need to propagate this action, so we will always be helping and getting help from each other, I wish that good be with you, your families and friends.
Richard MacCutchan 16-Aug-21 10:14am    
That comment was posted nearly four years ago, and the person who posted it has closed their account. So I am not sure why you are concerned about it.

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