How to export an Excel file using ANGULAR5 in ASP MVC

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.