Generate excel finishing (set the border / font / color / bold / middle /)

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFCellStyle setBorder = wb.createCellStyle();
First, set the background color:
setBorder.setFillForegroundColor((short) 13);// 设置背景色
setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
Second, set the border:
setBorder.setBorderBottom (HSSFCellStyle.BORDER_THIN); // bottom border
setBorder.setBorderLeft (HSSFCellStyle.BORDER_THIN); // left border
setBorder.setBorderTop (HSSFCellStyle.BORDER_THIN); // on the border
setBorder.setBorderRight (HSSFCellStyle.BORDER_THIN); // right border
Third, set the center:
setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
Fourth, set the font:
Font = wb.createFont HSSFFont ();
font.setFontName ( "bold");
font.setFontHeightInPoints ((Short) 16); // set the font size
Font2 = wb.createFont HSSFFont ();
font2.setFontName ( "italics _GB2312");
font2.setBoldweight (HSSFFont.BOLDWEIGHT_BOLD); // bold
font2.setFontHeightInPoints ((short) 12);
setBorder.setFont (font); // Select the font used in the format required
Five, set the column width:
sheet.setColumnWidth (0, 3766); // first parameter represents a column id (starts from 0), the second parameter represents the value of the width
Sixth, set Wrap:
setBorder.setWrapText (true); // Set wrap
Seven, merge cells:
Region region1 = new Region(0, (short) 0, 0, (short) 6);
// Parameter 1: 2 line number parameters: the starting column number 3 parameters: Parameter line number 4: termination column number
sheet.addMergedRegion (region1);
Or use
CellRangeAddress region1 = new CellRangeAddress(rowNumber, rowNumber, (short) 0, (short) 11);
It should be noted two constructor parameters are not the same, different versions of which depends on the specific use of the POI.
sheet.addMergedRegion (region1);
Currently used so much, the follow-up will continue to add new.
Eight, add borders
  HSSFCellStyle
 CellStyle wookBook.createCellStyle = ();
  cellStyle.setAlignment (HSSFCellStyle.ALIGN_CENTER);
  cellStyle.setBorderBottom (HSSFCellStyle.BorderBORDER_MEDIUM);
  cellStyle.setBottomBorderColor (HSSFColor.BLACK.index);
  cellStyle.setBorderLeft (HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setLeftBorderColor (HSSFColor.BLACK.index);
  cellStyle.setBorderRight (HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setRightBorderColor (HSSFColor.BLACK.index);
  cellStyle.setBorderTop (HSSFCellStyle.BORDER_MEDIUM);
  cellStyle.setTopBorderColor (HSSFColor.BLACK.index);
- --------------
Disclaimer: this article is CSDN blogger "sucking cow" of the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and this link statement.
Original link: https: //blog.csdn.net/sinat_34093604/article/details/53432545

Guess you like

Origin www.cnblogs.com/xueblvip/p/12071245.html