NPOI generates and downloads excel files, and NPOI generates and downloads excel files

Source: Internet
Author: User

NPOI generates and downloads excel files, and NPOI generates and downloads excel files

NPIO file: http://npoi.codeplex.com/

Directly reference the file to the project ,,,,,

Although there are a lot of online materials, you may not be able to find the functions you want. Today, I am idle, so I just sorted out a simple example and hope I can use them in future projects, you don't need to search for the code on the Internet. just copy the code ~~~~ I typed it in the mvc controller and pasted the Code directly .....

1,Logic code

Public EmptyResult ExportExcle () {HSSFCellStyle style; HSSFFont font; # if DEBUG UserInfo u = new UserInfo (); List <UserInfo> list = u. getDate (); string url = Server. mapPath (@ "\ Content \ File \ MyBook.xls"); string sheetName = "MySheet"; # endif HSSFWorkbook hssfworkbook = new HSSFWorkbook (); FileStream filecreate = new FileStream (url, FileMode. create, FileAccess. readWrite); // create a worksheet HSSFSheet sheet = hssfworkbook. createSheet (sheetName) as HSSFSheet; IRow row = sheet. createRow (0); row. createCell (0 ). setCellValue ("User ID"); row. createCell (1 ). setCellValue ("username"); row. createCell (2 ). setCellValue ("gender"); row. createCell (3 ). setCellValue ("Age"); row. createCell (4 ). setCellValue ("phone"); row. createCell (5 ). setCellValue ("ID card"); // row Height row. heightInPoints = 20; // set the style (alignment, border, Font, and background color) for the header cell List <ICell> cell = row. cells; style = hssfworkbook. createCellStyle () as HSSFCellStyle; font = hssfworkbook. createFont () as HSSFFont; font. isBold = true; // bold font. fontName = ""; font. color = HSSFColor. red. index; // font color style. setFont (font); this. cellStyle (style, sheet); style. fillForegroundColor = NPOI. HSSF. util. HSSFColor. green. index; style. fillPattern = FillPattern. solidForeground; cell. forEach (delegate (ICell c) {c. cellStyle = style ;}); // loads the content if (list. any () {style = hssfworkbook. createCellStyle () as HSSFCellStyle; this. cellStyle (style, sheet); for (int I = 0; I <list. count; I ++) {row = sheet. createRow (I + 1); row. heightInPoints = 20; row. createCell (0 ). setCellValue (list [I]. userId); row. createCell (1 ). setCellValue (list [I]. userName); row. createCell (2 ). setCellValue (list [I]. sex); row. createCell (3 ). setCellValue (list [I]. age); row. createCell (4 ). setCellValue (list [I]. tel); row. createCell (5 ). setCellValue (list [I]. idCard); cell = row. cells; cell. forEach (p => p. cellStyle = style) ;}}// write the stream to the excel file hssfworkbook. write (filecreate); filecreate. close (); # region download file FileStream fileopen = new FileStream (url, FileMode. open); byte [] bytes = new byte [(int) fileopen. length]; fileopen. read (bytes, 0, bytes. length); Response. contentType = "application/octet-stream"; Response. addHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode ("MyBook.xls", System. text. encoding. UTF8); Response. binaryWrite (bytes); fileopen. close (); Response. flush (); Response. end (); return new EmptyResult (); # endregion}

 2. Cell Style

/// <Summary> /// style /// </summary> /// <param name = "style"> </param> /// <param name =" sheet "> </param> private void CellStyle (HSSFCellStyle style, HSSFSheet) {// wrap style automatically. wrapText = true; // border style. borderBottom = BorderStyle. thin; style. borderLeft = BorderStyle. thin; style. borderRight = BorderStyle. thin; style. borderTop = BorderStyle. thin; // alignment style. alignment = HorizontalAlignment. center; style. verticalAlignment = verticalignment. center; // set the width of the fourth and fifth columns. setColumnWidth (4, 20*256); sheet. setColumnWidth (5, 30*256 );}

3. Data sources

Private class UserInfo {public int UserId {get; set;} public string UserName {get; set;} public string Sex {get; set;} public int Age {get; set ;} public string Tel {get; set;} public string IdCard {get; set;} public List <UserInfo> GetDate () {List <UserInfo> list = new List <UserInfo> () {new UserInfo {UserId = 1, UserName = "Zhang San", Sex = "male", Age = 20, Tel = "18217722343", IdCard = "150726198810235436 "}, new UserInfo {UserId = 2, UserName = "", Sex = "female", Age = 23, Tel = "18217722343", IdCard = "150726198810235436 "}, new UserInfo {UserId = 3, UserName = "", Sex = "male", Age = 21, Tel = "18217722343", IdCard = "150726198810235436 "}, new UserInfo {UserId = 4, UserName = "Zhao ", Sex = "female", Age = 30, Tel = "18217722343", IdCard = "150726198810235436 "}, new UserInfo {UserId = 5, UserName = "Qian Qi", Sex = "male", Age = 45, Tel = "18217722343", IdCard = "150726198810235436 "}, new UserInfo {UserId = 6, UserName = "Zhang San", Sex = "female", Age = 18, Tel = "18217722343", IdCard = "150726198810235436 "}}; return list ;}}

4. Front-end code

I encountered a problem before, because at first I used the Ajax Method for submission, so I could not see the download prompt box in the browser. Finally, I spent half a day, because the submission method was incorrect, change to location.

<! DOCTYPE html> 

5. Foreground page Effects

 

6. Open Excel

Well, I will introduce it here. I hope it will be helpful to you ~~~~

 

Related Article

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.