Npoi's C # download Excel

Source: Internet
Author: User

This class library in Java is called poi,c# Npoi, a lot of classes from Java to. NET platform for the difference is mostly in front of a n, such as Hibernate and NHibernate.

Npoi

First, the use of Npoi download roughly the following steps:

1. Create Workbook

2. Create sheet

3. Create row and cell

4. Fill data

5. Set the cell's style and font

Two, Hssfworkbook and Xssfworkbook both use basically the same

Hssfworkbook was created by Excel2003

Hssfsheet, Hssfcellstyle, Hssffont, etc.

Xssfworkbook was created by Excel2007

Xssfsheet, Xssfcellstyle, Xssffont, etc.

Three, row or column merge

Sheet. Addmergedregion (New cellrangeaddress (0, 1, 0, 0)); Start row end row Start column end column

Sheet. GetRow (0). Getcell (0). Setcellvalue ("name");//Assignment

You must use the row and column coordinates of the upper-left cell of the merge when assigning values

Iv. code (take Excel2003 as an example):

Using Npoi. HSSF. Usermodel;
Using Npoi. HSSF. Util;

 PublicActionResult Index () {List<string> headlist =Newlist<string>(); Headlist.add ("language"); Headlist.add ("Mathematics"); Headlist.add ("English"); Headlist.add ("political"); List<string> namelist =Newlist<string>(); Namelist.add ("Zhang San"); Namelist.add ("John Doe"); Namelist.add ("Harry"); Namelist.add ("Zhao Liu"); Namelist.add ("win seven"); Namelist.add ("Zhu Weight Eight"); Namelist.add ("Nine Lama"); MemoryStream MemoryStream=NewMemoryStream (); Hssfworkbook Workbook=NewHssfworkbook (); Hssfsheet sheet=workbook.            Createsheet (); //Create a Cell settings objectHssfcellstyle CellStyle =workbook.            Createcellstyle (); //set Horizontal, vertical centeringCellstyle.alignment =Hssfcellstyle.align_center; Cellstyle.verticalalignment=Hssfcellstyle.vertical_center; //Cell Fill ColorCellstyle.fillforegroundcolor =HSSFColor.PALE_BLUE.index; Cellstyle.fillpattern=Hssfcellstyle.solid_foreground; //Set BorderCellstyle.borderbottom =Hssfcellstyle.border_thin; Cellstyle.borderleft=Hssfcellstyle.border_thin; Cellstyle.borderright=Hssfcellstyle.border_thin; Cellstyle.bordertop=Hssfcellstyle.border_thin; //Create a set Font objectHssffont Font =workbook.            CreateFont (); Font. Fontheightinpoints= -;//Set Font sizeFont. Boldweight = Short. MaxValue;//BoldFont. FontName ="Song Body";            Cellstyle.setfont (font); //Create Excel rows and cells             for(inti =0; I < Namelist.count +2; i++) {Hssfrow NewRow=sheet.                CreateRow (i);  for(intj =0; J 1; J + +) {Newrow.createcell (j); }            }            //set up an Excel table headerSheet. Addmergedregion (NewCellrangeaddress (0,1,0,0));//Start row end row Start column end columnSheet. GetRow (0). Getcell (0). CellStyle =CellStyle; Sheet. GetRow (1). Getcell (0). CellStyle =CellStyle; Sheet. GetRow (0). Getcell (0). Setcellvalue ("name");//namesheet. Addmergedregion (NewCellrangeaddress (0,0,1,4));//Start row end row Start column end columnSheet. GetRow (0). Getcell (1). CellStyle =CellStyle; Sheet. GetRow (0). Getcell (4). CellStyle =CellStyle; Sheet. GetRow (0). Getcell (1). Setcellvalue ("Courses");//Courses             for(inti =0; i < Headlist.count; i++) {sheet. GetRow (1). Getcell (i +1). CellStyle =CellStyle; Sheet. GetRow (1). Getcell (i +1). Setcellvalue (Headlist[i]);//Specific Courses            }            //Import Data             for(inti =0; i < Namelist.count; i++) {sheet. GetRow (i+2). Getcell (0).                Setcellvalue (Namelist[i]);  for(intj =0; J <4; J + +) {sheet. GetRow (i+2). Getcell (j+1). Setcellvalue (i*j); }} workbook.            Write (MemoryStream); Memorystream.seek (0, Seekorigin.begin);            Response.Clear (); Response.AddHeader ("content-disposition","attachment;filename="+ Server.URLEncode ("Test.xls")); Response.ContentType="Application/octet-stream"; Response.Charset="gb2312"; Response.ContentEncoding=Encoding.UTF8;            Response.BinaryWrite (Memorystream.getbuffer ());            Response.Flush ();            Response.End (); return NewEmptyresult (); }

Final Result:

Reprint please specify the source!

Npoi's C # download 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.