c# - Export to Excel as a response in web API -
i using web api 4.5.1. generating api in c# code , passing api front end, front end built on html , angularjs javascript. requirement create api export excel front end. kind of stuck here. receiving request front end people fromdate , todate. based on query database data , if successful, html format out of it. unable go further new this. below code trying achieve 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 html format data required. // unable find solution here }
how can complete operation?
can try this?
var response = new httpresponsemessage(system.net.httpstatuscode.ok); //if have physical file read filestream , use it. response.content = new streamcontent(filestream); //or //create file on fly , file data byte array , send client response.content = new bytearraycontent(filebytes);//use 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 works fine.please let me know if have doubts on this.
Comments
Post a Comment