1. Export Excel
Publicactionresult Export (formcollection frm) {DataTable DTS=NewDataTable (); DTS=_shopmemeber.exportmemberdata (frm); Iworkbook Workbook=NewXssfworkbook (); Isheet sheet=workbook. Createsheet (); IRow HeaderRow= Sheet. CreateRow (0); foreach(DataColumn columninchDTS. Columns) Headerrow.createcell (column. Ordinal). Setcellvalue (column. Caption); intRowIndex =1; foreach(DataRow rowinchDTS. Rows) {IRow DataRow=sheet. CreateRow (RowIndex); foreach(DataColumn columninchDTS. Columns) {Datarow.createcell (column. Ordinal). Setcellvalue (Row[column]. ToString ()); } RowIndex++; } stringfilepath = Server.MapPath ("/") +@"List of users. xlsx"; FileStream file=NewFileStream (filepath, filemode.create); Workbook. Write (file); Excelhelper.download (@"/user list. xlsx"); #regionNot enabled#endregion returnSuccessmsg ("Adminmembermemberindex"); }//This is the way to download to the desktop, do not implement the optional path Public Static voidDownLoad (stringFileName) {FileInfo FileInfo=NewFileInfo (HttpContext.Current.Server.MapPath (FileName)); //download a file as a stream of charactersFileStream fs =NewFileStream (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"; //notifies the browser to download a file instead of opening itHttpContext.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 (); }View Code
2 Export
Public BOOLImpoart (httppostedfilebase file) {Try { //Save Excel stringPath = HttpContext.Current.Server.MapPath ("/"); File. SaveAs (Path+file. FileName); //ReadFileStream SW= File.Open (path +file. FileName, FileMode.Open, FileAccess.Read); Iworkbook Workbook=NewXssfworkbook (SW); Isheet Sheet1= Workbook. Getsheet ("Sheet1"); //Maximum number of rows intRowscount =Sheet1. Physicalnumberofrows; //determine if the first row conforms to the specification, which is the column name in ExcelIRow FirstRow = Sheet1. GetRow (0); if ( ! (Firstrow.getcell (0). ToString () = ="name"&& Firstrow.getcell (1). ToString () = ="abbreviation"&&Firstrow.getcell (2). ToString () = ="category"&& Firstrow.getcell (3). ToString () = ="Reference Price"&&Firstrow.getcell (4). ToString () = ="Product Introduction")) { return false; } //skipping a product of the wrong type for(inti =1; i < Rowscount; i++) {IRow row=Sheet1. GetRow (i); Shop_product Product=Newshop_product (); stringCategory = row. Getcell (2) !=NULL? Row. Getcell (2). ToString ():NULL; if(!string. IsNullOrEmpty (category)) {varCate =_unitofwork.shop_productcategoryrepository (). GetAll (). FirstOrDefault (t= = T.name = =category); if(Cate! =NULL) {product. Productcategoryname=Cate. Name; Product. shop_productcategory_id=cate.id; } Else { Continue; } } Else { Continue; } 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) {product. Price= Double.Parse (row. Getcell (3). ToString ()); } product. Description= row. Getcell (4) !=NULL? Row. Getcell (4). ToString ():NULL; _unitofwork.shop_productrepository (). Insert (product); } _unitofwork.save (); } Catch { return false; } return true; }View Code
C # import and Export execl