ASP. NET MVC export Excel

Source: Internet
Author: User

Front-End Code:

1 @Html. ActionLink (" export Excel""exportwarehouseinoutdetailtable  "new {warehouseinoutid = model.id},new"btn btn-primary pull-right "})

Back-end Code

1  Public voidExportwarehouseinoutdetailtable (LongWarehouseinoutid)2         {3 response.clear ();4             if(Warehouseinoutid <=0)5             {6Response.Write ("<script>confirm (' No query to any data! ') </script>");7                 return;8             }9             varModel =_service. Detailincludedetailsandgoods (Warehouseinoutid);Ten             if(model. IsNull () | | Model. Id <=0) One             { AResponse.Write ("<script>confirm (' No query to any data! ') </script>"); -                 return; -             } the  -Response.ContentType ="Application/vnd.ms-excel"; -             stringFileName =string. Format ("{0}_{1}.xls", model. Storagestatus.getenumdesc (), model. Serialid); -Response.AddHeader ("content-disposition",string. Format ("attachment;filename={0}", FileName +". xls")); + response.clear (); -             varWorkBook =_service. Exportwarehouseinoutdetailtable (model); +             varms =NewMemoryStream (); A Workbook.write (MS); at Response.BinaryWrite (Ms. GetBuffer ()); -             #regionOld code returns file, commented -             ////if (Warehouseinoutid<=0)//return empty file - //var model = _service. Detailincludedetailsandgoods (Warehouseinoutid); -             ////if (model. IsNull () | | Model. Id<= 0)//return empty file - //var WorkBook = _service. Exportnpoiexcelwookbook (model); in             //var ms = new MemoryStream (); -             //Workbook.write (MS); to             //Ms. Seek (0, seekorigin.begin); +             //method Specifies the return Fileresult -             //return File (MS, "application/vnd.ms-excel" ) the             #endregion, String. Format ("{0}_{1}.xls", model.) Storagestatus.getenumdesc (), model. Serialid)); *         } $ Panax Notoginseng  PublicHssfworkbook exportwarehouseinoutdetailtable (warehouseinoutcontract model) -         { theHssfworkbook WorkBook =NewHssfworkbook (); +Isheet Sheet1 = Workbook.createsheet ("Sheet1"); AIcellstyle Centercellstyle =Workbook.createcellstyle (); theCentercellstyle.verticalalignment =Verticalalignment.center; +Centercellstyle.alignment =HorizontalAlignment.Center; -Icellstyle Centerboldcellstyle =Centercellstyle; $Hssffont Font =(Hssffont) Workbook.createfont (); $Font. Boldweight = ( Short) Fontboldweight.bold; - Centerboldcellstyle.setfont (font); -             intRownumindex =0; the  -IRow Row1 = Sheet1. CreateRow (rownumindex++);Wuyi             varCelltitle = Row1. Createcell (0); the Celltitle.setcellvalue (model. Storagestatus.getenumdesc ()); -Celltitle.cellstyle = Centerboldcellstyle;//The style must be specified separately to the cell element, and directly to the row is invalid: Row1. RowStyle = Centerboldcellstyle;  WuSheet1. Addmergedregion (NewNpoi. Ss. Util.cellrangeaddress (0,0,0,5)); -  AboutIRow Row2 = Sheet1. CreateRow (rownumindex++); $Row2. Createcell (0). Setcellvalue ("Document Number");//TODO: Getdisplayattrname to be implemented (model. Serialid) -Row2. Createcell (1). Setcellvalue (model. Serialid); -Row2. Createcell (2). Setcellvalue ("creation Time"); -Row2. Createcell (3). Setcellvalue (model. CreatedOn); A             if(!model. Isoutwarehouse) +             { theRow2. Createcell (4). Setcellvalue ("Total price of this batch"); -Row2. Createcell (5). Setcellvalue (decimal. ToDouble (model. Totalprice)); $             } the  theIRow row3 = Sheet1. CreateRow (rownumindex++); theRow3. Createcell (0). Setcellvalue ("Warehouse Administrator Name"); theRow3. Createcell (1). Setcellvalue (model. Warehousehandlername); -Row3. Createcell (2). Setcellvalue ("Buyer or picking member"); inRow3. Createcell (3). Setcellvalue (model. Outhandlername); theRow3. Createcell (4). Setcellvalue ("name of the owning warehouse"); theRow3. Createcell (5). Setcellvalue (model. Warehouseareaname); About  theIRow row4 = Sheet1. CreateRow (rownumindex++); theRow4. Createcell (0). Setcellvalue ("Item Name"); theRow4. Createcell (1). Setcellvalue ("Unit name"); +Row4. Createcell (2). Setcellvalue ("type name"); -Row4. Createcell (3). Setcellvalue ("Total Price"); theRow4. Createcell (4). Setcellvalue ("Quantity");BayiRow4. Createcell (5). Setcellvalue ("Unit Price"); theRow4. Createcell (6). Setcellvalue ("Notes"); the             foreach(varDetinchmodel. Warehouseinoutdetails) -             { -IRow Rowdet = Sheet1. CreateRow (rownumindex++); theRowdet.createcell (0). Setcellvalue (Det. Goods.name); theRowdet.createcell (1). Setcellvalue (Det. Goods.unitname); theRowdet.createcell (2). Setcellvalue (Det. Goods.typename); theRowdet.createcell (3). Setcellvalue (decimal. ToDouble (Det. Totalprice)); -Rowdet.createcell (4). Setcellvalue (Det. Quantity); theRowdet.createcell (5). Setcellvalue (decimal. ToDouble (Det. UnitPrice)); theRowdet.createcell (6). Setcellvalue (Det. Remark); the             }94             returnWorkBook; the}

ASP. NET MVC export Excel

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.