Words do not say much, directly to the actual.
Import {injectable} from ' @angular/core ';
Import {HttpClient, Httpparams, httpheaders} from ' @angular/common/http ';
Import {Observable} from ' Rxjs ';
@Injectable ()
The base component is referenced first.
url:string;
Constructor (private http:httpclient) {}
Declaring API Path Access
Initializes the HttpClient object when the object is instantiated.
About exporting Excel This block, I use the file stream, different ideas of friends can refer to
Background C # code:
Public Fileresult Export (int itype = 0)
{
Expression<func<t_carregister, bool>> Func =p=> true;
if (Itype > 0)
{
Func = Func.and (U = u.f_cartype = = Itype);
}
var list = _crs. Getconditionbywheretoexport (Func);
DataSet ds = new DataSet ();
DataTable dt = datatableext.todatatable (list);
Ds. Tables.add (DT);
MemoryStream stream = npoiexcelhelper.exportexcel (dt, NPOIExcelHelper.ExtendPropertiesType.None, "T_carregister");
Return File (Stream, "application/vnd.ms-excel", "Vehicle Management" + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". xls");
}
This is used to npoi the third-party Excel export component, convert the queried data into Excel form, and then return it as a file stream.
Now look at the front desk JS processing this piece:
Export Excel
Excelexport (itype:string) {
var now = new Date ()
This.url = "/carregister/export?itype=" + itype;
Return This.downloadfile ("Vehicle Management" + now.getdate () + ". xls");
}
This is calling HTTP to send requests to the background
Dodownload () {
Return This.http.get (This.url, {headers:new httpheaders (). Append (' Content-type ', ' application/vnd.ms-excel '), Responsetype: ' blob ', observe: ' Body '})
}
Store the file stream returned in the background as a file type that needs to be saved
DownloadFile (filename:string) {
Return This.dodownload (). Subscribe (
res = {
var url = window. Url.createobjecturl (RES);
var a = document.createelement (' a ');
Document.body.appendChild (a);
A.setattribute (' style ', ' display:none ');
A.href = URL;
A.download = filename;
A.click ();
Window. Url.revokeobjecturl (URL);
A.remove ();
}, Error = {
Console.log (' Download error: ', json.stringify (Error));
}, () = {
Console.log (' completed file download. ')
});
}
This piece of thinking, is to query the Dodownload method after the completion of the object processing. Get the file stream object, create the file stream object, then use a tag to click on him, get this object, then click on the a tag, trigger the download of the mechanism, and then download Excel to Local.
Currently for ASP MVC this piece of export Excel is such a son, what doubts can leave a message, or add me QQ together communication. qq:1024358044
How to export an Excel file using ANGULAR5 in ASP MVC