java整合POI填充样式记录
- 添加依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
2.给数据填充边框,本次使用是在所有数据填充完成后进行补充边框
CellStyle style = workbook.createCellStyle();
style.setBorderTop(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
for (int rowNum = firstRow; rowNum <= lastRow; rowNum++) {
Row row = sheet.getRow(rowNum);
if (null != row) {
for (int colNum = firstCell; colNum <= lastCell; colNum++) {
Cell cell = row.getCell(colNum);
if (null == cell) {
cell = row.createCell(colNum);
}
cell.setCellStyle(style);
}
}
}
3.设置宽度自适应
SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet("数据信息统计");
#关键是使用SXSSFSheet,这个类中存在方法可以追踪到有多少个行信息以及列信息
sheet.trackAllColumnsForAutoSizing();
sheet.autoSizeColumn(0); #这个列下标根据实际情况进行修改
style.setWrapText(true);#这个设置自动换行