Recently because of the project contact Npoi, feeling still pretty good, the online tutorial general version is older, this article records commonly used operation, uses Npoi 2.0 version.
Recommendation: Npoi official website npoi 1.2.4/1.2.5 Official tutorial
New Excel
hssfworkbook Hssfworkbook =newhssfworkbook(); Isheet Sheet1 = hssfworkbook. Createsheet("Sheet1"); Establish Sheet1
Save (export)Excel FileStream File =newFileStream(@"E:\HSSFWorkbook.xls",FileMode. Create); Hssfworkbook. Write(file); file. Close();
import Excel FileStream File =newFileStream(@"Template/template.xls",FileMode. Open,fileaccess. Read); Hssfworkbook =newhssfworkbook(file);
Add textIcell cell1 =hssfcellutil. Createcell(Sheet1. CreateRow(0),0,"A1"); Add A1 to A1 cells and instantiate Row0 Icell cell2 = Hssfcellutil.createcell (Sheet1. GetRow (0), 1, "B1");//Add B1 to B1 unit, this method requires Row0 instantiation before Sheet1.getrow (0) can be used. Createcell (2). Setcellvalue ("C1");//Add C1 to C1 unit, this method requires Row0 instantiation to use Sheet1.createrow (1). Createcell (0). Setcellvalue ("A2");//Add A2 to A2 unit and instantiate Row1Note When adding text to the cell instantiation problem, if in the same cell, after multiple instantiation, will overwrite the text of the peer. Provides two ways to add text, each with its pros and cons.
Set font formatIFont font1 = hssfworkbook. CreateFont(); font1. FontName="song Body"; Font Font1. fontheightinpoints = 20;//font size font1. color = hssfcolor.red.index;//colors font1. Boldweight = 700;//Bold font1. Isitalic = true;//Italic font1. underline = (byte) fontunderlinetype.double;//add double underlineicellstyle style1 = hssfworkbook. Createcellstyle(); style1. SetFont(font1); The font format is bound to the style, and the style contains settings such as font formatting, color, border, and so on when the style is set and assigned to the cell. cell1. CellStyle= style1; Sheet1. GetRow(1). Getcell(0). CellStyle= style1;
Merge cells
Sheet1. Addmergedregion(newcellrangeaddress(2,3,0,1)); Merging a3-b4
Cellrangeaddress (starting line, terminating line, starting column, terminating column);
Add a border
IcellstyleStyle2=Hssfworkbook.Createcellstyle();Style2.BorderBottom=Npoi.Ss.Usermodel.BorderStyle.THIN;Style2.Borderleft=Npoi.Ss.Usermodel. BorderStyle. THIN; Style2. BorderRight= npoi. SS. Usermodel. BorderStyle. THIN; style2. BorderTop= npoi. SS. Usermodel. BorderStyle. THIN;
Add slash
Style2. Borderdiagonal = Borderdiagonal.backward; style2. Borderdiagonallinestyle= npoi. SS. Usermodel. BorderStyle. THIN;
Cell2. CellStyle= style2;
Add a border to describe both the upper and lower left and right borders.
Set alignment Related Settings
IcellstyleStyle3=Hssfworkbook.Createcellstyle style3. Alignment= npoi.. Usermodel. Horizontalalignment. Center; Center style3. Verticalalignment=verticalalignment.; Center vertically style3. Wraptext=true; Wrap
Sheet1. GetRow(0). Getcell(2). CellStyle= style3;
Reprinted from: http://anchises.blog.163.com/blog/static/6432108220129211423620/
"Go". NET Npoi Operations Excel Common functions