Npoi In fact has been introduced almost, and then pasted a convenient later copy and paste.
The highlight is actually server.mappath this thing, can find the MVC project under the folder, find the path is very convenient.
/// <summary> ///device export Excel table/// </summary> PublicFileresult Exportexcel () {stringFiledir = Server.MapPath ("~/reporttemplate/my template. xls"); FileStream Dir=NewFileStream (Filedir, FileMode.Open, FileAccess.Read); Hssfworkbook Workbook=NewHssfworkbook (Dir); Isheet sheet= Workbook. Getsheet ("Sheet1"); MemoryStream Ms=NewMemoryStream (); DataTable Table= Eqequipmentbll.getinstance (). Getlistforexport (""); for(inti =2; I < table. Rows.Count; i++) {sheet. GetRow (i). Getcell (0). Setcellvalue (table. rows[i-2]["eqid"]. ToString ()); Sheet. GetRow (i). Getcell (1). Setcellvalue (table. rows[i-2]["Eqname"]. ToString ()); Sheet. GetRow (i). Getcell (2). Setcellvalue (table. rows[i-2]["producefactory"]. ToString ()); Sheet. GetRow (i). Getcell (3). Setcellvalue (table. rows[i-2]["Model"]. ToString ()); } sheet. Forceformularecalculation=true;//force calculation of formulas in Excelworkbook. Write (MS); Ms. Flush (); Ms. Seek (0, Seekorigin.begin); returnFile (MS,"Application/ms-excel", $"My Templates{DateTime.Now.ToString ("D")}.xls"); }
. NET MVC uses Npoi to read Excel templates and then write data