Npoi Create Excel set width style color comparison table

Source: Internet
Author: User

The first two days with Npoi to operate Office software, in the use of a bit of a problem, there is a harvest, make a note to record down, the main thing to do the export of data functions. Some public way, make a note. More details can be found in Npoi's official tutorials to see http://tonyqus.sinaapp.com/

1. Quoting Npoi

This, using a third-party class library to add DLLs, I am using 1.2.5 version, the specific version can be found in the genus. It looks like the latest version is 2.0.

DLL file click to download

2. Create a simple Excel

MemoryStream ms =New MemoryStream ();//Create a memory stream//Create a fileString fileName ="D://xinjian.xlsx"; Iworkbook Workbook =New Hssfworkbook ();//The namespaces of Iworkbook and Hssfworkbook are NPOI.HSSF.UserModel respectively; Npoi. Ss. Usermodel; Remember to quote isheet Sheet1 = workbook. Createsheet ("Sheet1");//Create a workbook in parentheses for the name IRow DataRow = Sheet1. CreateRow (1);//Creating a data row 1 means creating a variable Datarow.createcell in the first line (1). Setcellvalue ("First row first column ");  Create a cell and assign a value of workbook. Write (MS); Ms. Flush (); Ms. Position = 0; using (FileStream fs = new FileStream (FileName, FileMode.Create, FileAccess.Write)) { byte[] data =  Ms. ToArray (); Fs. Write (data, 0, data. Length); Fs. Flush (); data = null;}                

3. Get Excel Data

4. Setting the height

20; the unit of////height is 1/20 points, so the value of height is always 20 times times that of heightinpoints. 

5. Setting the width

Sheet1. Setcolumnwidth (n);  Set width /// Here you will find an interesting phenomenon, the second parameter of Setcolumnwidth is multiplied by 256, what is going on? In fact, the unit of this parameter is 1/256 character width, that is, the width of column B is set to 100 characters. 

6. Set Style

Headerrow.getcell (1). CellStyle = Getcellstyle (Workbook,"Header");//Set style//Cell styleStatic Icellstyle Getcellstyle (Iworkbook WB,StringType) {//Hssfcellstyle cels = new Hssfcellstyle (1, Icellstyle CellStyle =Wb. Createcellstyle (); Cellstyle.alignment =Npoi. Ss. UserModel.HorizontalAlignment.CENTER;Switch(type) {Case"Header": Cellstyle.verticalalignment =Npoi. Ss. UserModel.VerticalAlignment.TOP; Cellstyle.wraptext =True//Wrap LineBreak;Case"Program": Cellstyle.fillpattern =Fillpatterntype.solid_foreground; Cellstyle.fillforegroundcolor =26;Break;Case"GroupName": Cellstyle.fillpattern =Fillpatterntype.solid_foreground; Cellstyle.fillforegroundcolor =; break; case "columnName": Cellstyle.fillpattern = fillpatterntype.solid_foreground; Cellstyle.fillforegroundcolor = ; break;} return CellStyle;} The  background color of the cells that are filled here is not fillbackgroundcolor but Fillforegroundcolor is strange and must be set FillPattern this property    

7. Color comparison Table

Finally, to provide you with an operation Npoi Simple class library, hope to have some help click to download

Npoi Create Excel set width style color comparison table

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.