JAVA POI3.17 Excel 表格居中,加边框,给边框加颜色

关于POI3.17的一些操作

单元格居中

HSSFCellStyle tableStyle=workbook.createCellStyle();
tableStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
tableStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中

加边框

HSSFCellStyle tableStyle=workbook.createCellStyle();
tableStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
tableStyle.setBorderBottom(BorderStyle.MEDIUM);//下边框
tableStyle.setBorderLeft(BorderStyle.MEDIUM);//左边框
tableStyle.setBorderRight(BorderStyle.MEDIUM);//右边框

设置边框颜色

可以这样:

tableStyle.setBottomBorderColor(HSSFColorPredefined.BLUE.getIndex());//下边框颜色
tableStyle.setLeftBorderColor(HSSFColorPredefined.BLUE.getIndex());//左边框颜色
tableStyle.setRightBorderColor(HSSFColorPredefined.BLUE.getIndex());//右边框颜色

因为颜色都是short值,不妨把常用颜色的short值封装起来,方便使用,就像这样:
在这里插入图片描述
这样代码就简洁多了

tableStyle.setTopBorderColor(HSSFDiyColor.BLACK);
发布了24 篇原创文章 · 获赞 10 · 访问量 1303

猜你喜欢

转载自blog.csdn.net/qq_41625102/article/details/104203768