This article mainly introduced the ASP.net export Excel simple Method Example, needs the friend to be possible to refer to under
Excel's operation, the most commonly used is the export and import, nonsense not much to say on the code. This example uses Npoi to realize, do not like to spray ha .... Code as follows:///<summary> ///export Excel ///</summar y> ///<param name= "stime" ></param> ///<param N Ame= "ETime" ></param> ///<returns></returns> Public ActionResult Export (formcollection frm) { &NB Sp DataTable DTS = new DataTable (); DTS = _shopmemeber.exportmemberdata (frm); Iworkbook workbook = new Xssfworkbook (); Isheet sheet = workbook. Createsheet (); irow headerrow = sheet. CreateRow (0); foreach (DataColumn column in DTS. Columns) &NBsp Headerrow.createcell (column. Ordinal). Setcellvalue (column. Caption); int rowIndex = 1; foreach (DataRow row in DTS. Rows) { IRow DataRow = s Heet. CreateRow (RowIndex); foreach (DataColumn column in DTS. Columns) Datarow.createcell (column. Ordinal). Setcellvalue (Row[column]. ToString ()); RowIndex ++; String filepath = Server.MapPath ("/" + @ "user list. xlsx"; FileStream file = new FileStream (filepath, filemode.create); workbook. Write (file); Excelhelper.download (@ "/user list. xlsx"); #region not enabled #endregion & nbsp return successmsg ("Adminmembermemberindex"); }//This is the way to download to the desktop, not implementing the optional path public static void DownLoad (String FileName) { &NB Sp fileinfo FileInfo = new FileInfo (HttpContext.Current.Server.MapPath (FileName)); //in the form of character streams filestream F s = new FileStream (HttpContext.Current.Server.MapPath (FileName), FileMode.Open); byte[] bytes = new byte[(int) fs. Length]; FS. Read (bytes, 0, bytes. LenGTH); FS. Close (); HttpContext.Current.Response.ContentType = "Application/octet-stream"; //notification browser download file not open Httpcont Ext. Current.Response.AddHeader ("Content-disposition", "attachment; filename= "+ httputility.urlencode (Fileinfo.name, System.Text.Encoding.UTF8)); HttpContext.Current.Response.BinaryWrite (bytes); httpcontext.current.response.flush (); HttpContext.Current.Response.End (); } above is exported, below I introduce under import. Code as follows:///<summary> ///import data ///</SUMMARY&G T ///<param name= "file" ></param> ///<returns>tRue import success </returns> public bool Impoart (httppostedfilebase file) &N Bsp { try { &N Bsp //Save Excel string path = Httpcontext.current.s Erver. MapPath ("/"); file. SaveAs (path + file). FileName); /read &NBSP ; FileStream SW = File.Open (path + File. FileName, FileMode.Open, FileAccess.Read); Iworkbook workbook = new Xssfworkbook (SW); isheet sheet1 = workbook. Getsheet ("Sheet1"); //maximum lines &NBSp int rowscount = Sheet1. Physicalnumberofrows; //Judging whether the first line conforms to the specification is the column name in Excel, IRow firstrow = Sheet1. GetRow (0); if ( &NBSP ; ! (Firstrow.getcell (0). ToString () = = "Name" && Firstrow.getcell (1). ToString () = "short" && Firstrow.getcel L (2). ToString () = = "Classification" && Firstrow.getcell (3). ToString () = "Reference price" && FIRSTROW.GETCE LL (4). ToString () = "Product introduction") { return false; } //skip type incorrect items &NBS P for (int i = 1; i < Rowscount i++) { &NBS P irow row = Sheet1. GetRow (i); shop_product Product = new Shop_product (); String category = row. Getcell (2)!= null? Row. Getcell (2). ToString (): null; if (!string. IsNullOrEmpty (category)) { &NBS P var cate = _unitofwork.shop_productcategoryrepositoRy (). GetAll (). FirstOrDefault (t => t.name = = category); if (Cate!= null) product. Productcategoryname = Cate. Name; product. shop_productcategory_id = cate.id; & nbsp else &NBSP ; { Continue   ; { &NBSP; down E LSE { &NBS P continue; & nbsp product. PName = row. Getcell (0)!= null? Row. Getcell (0). ToString (): null; product. Pcname = row. Getcell (1)!= null? Row. Getcell (1). ToString (): null; if (row. Getcell (3)!= null) { &N Bsp product. Price = Double.Parse (row. Getcell (3). ToString ()); &NBSP product. Description = row. Getcell (4)!= null? Row. Getcell (4). ToString (): null; _unitofwork.shop_productrepository (). Insert (product); _ Unitofwork.save (); catch &N Bsp { return false; } return true; }