First, create a blank workbook for testing, create a blank worksheet in it, create a blank row in the table, create a cell in the row, and fill in the content:
// create a blank workbook iworkbook workbook = New hssfworkbook (); // In the workbook: create a blank worksheet isheet Sheet = workbook. createsheet (); // In the worksheet, create a row. The parameter is a row number, from 0 irow ROW = sheet. createrow (0);
// In the row: Create a cell. The parameter is the column number, counted from 0.IcellCell = row. createcell (0 );// Set the cell contentCell. setcellvalue ("Practice identification form");
Set the cell style:When setting a cell style, be sure to create a new style object for setting. Otherwise, the style of all cells in the worksheet will be set together. They should share a style object:
IcellstyleStyle = Workbook. createcellstyle ();// Set the cell style: horizontally align and centerStyle. Alignment =Horizontalalignment. Center;// Create a new font style object
IfontFont = Workbook. createfont ();// Set the font Bold Style
Font. boldweight =Short. Maxvalue; // use the setfont method to add the font style to the cell style.
Style. setfont (font );// Assign the new style to the cellCell. cellstyle = style;
Set the cell width and height:
Set the height of a cellThe actual setting is to set the row height, so you need to set the Row Height on the row where the cell is located. The Row Height setting value seems to be 1/20 of the pixel, so * 20 is used to achieve the setting effect;
Set the cell widthIn fact, it is to set the width of the column where the cell is located (the column is set on the worksheet). The width value seems to be 1/256 of the character, so * 256 is used to achieve the setting effect.
// Set the cell heightRow. Height = 30*20;// Set the cell width
Sheet. setcolumnwidth (0, 30*256 );
Merge cells:The merged cells actually declare an area. The cells in this area are merged. The merged content and style are subject to the cells in the upper left corner of the area.
// Set a cell merging area. Use the upper, lower, and lower layers to define the cellrangeaddress area. // four parameters are as follows: Start row, end row, start column, and end column.Sheet. addmergedregion (NewCellrangeaddress(0, 0, 0, 10 ));Add formula:Use cellformula of cell to set the formula. It is a string and NO = is required before the formula.
// Write the formula to the cell through cell cellformula // Note: You can directly write the formula content without adding '=' at the beginning'IcellCell2 = sheet. createrow (1). createcell (0); cell2.cellformula ="Hyperlink (\" test image .jpg \ ", \" test image .jpg \")";
Write the workbook to a file to view the effect:
// write the workbook to a file using ( filestream FS = New filestream ( " production efficiency .xls " , filemode . create, fileaccess . write) {workbook. write (FS) ;}
Final effect:
References:
Npoi: http://npoi.codeplex.com/
Npoi usage tutorial (Chinese): http://tonyqus.sinaapp.com/