POI 3.17 导出样式(与3.9方式不同)

HSSFWorkbook workbook = new HSSFWorkbook(); //创建工作簿
HSSFSheet sheet = workbook.createSheet(“Test”); //创建SHEET页
HSSFRow row = sheet.createRow(0); //创建行,从0开始
HSSFCell cell = row.createCell(0);//创建列,从0开始
cell.setCellValue(“姓名”);

设置样式

HSSFCellStyle style = workbook.createCellStyle();

加边框

style.setBorderBottom(BorderStyle.THIN);//下边框
style.setBorderLeft(BorderStyle.THIN);//左边框
style.setBorderRight(BorderStyle.THIN);//右边框
style.setBorderTop(BorderStyle.THIN); //上边框

居中

style.setAlignment(HorizontalAlignment.CENTER);//水平居中
style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中

设置字体

HSSFFont font = workbook.createFont();
font.setFontName(“华文行楷”);//设置字体名称
font.setFontHeightInPoints((short)28);//设置字号
font.setItalic(false);//设置是否为斜体
font.setBold(true);//设置是否加粗
font.setColor(IndexedColors.RED.index);//设置字体颜色
style.setFont(font);

设置背景

style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFillForegroundColor(IndexedColors.YELLOW.index);

设置宽度和高度

row.setHeightInPoints(30);//设置行的高度
sheet.setColumnWidth(0, 20 * 256);//设置列的宽度

渲染单元格

cell.setCellStyle(style);

猜你喜欢

转载自www.cnblogs.com/123-shen/p/11284563.html