添加poi依赖
<!-- excel工具 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
InputStream file = this.getClass().getClassLoader().getResourceAsStream("文件路径.xlsx");
Workbook excel = new XSSFWorkbook(file);
Sheet sheetAt = excel.getSheetAt(0);
Row row1 = sheetAt.getRow(1);
CellStyle cellStyle2 = excel.createCellStyle();
Font font = excel.createFont();
font.setBold(true);
cellStyle2.setFont(font);
cellStyle2.setBorderTop(BorderStyle.THIN);
cellStyle2.setBorderBottom(BorderStyle.THIN);
cellStyle2.setBorderLeft(BorderStyle.THIN);
cellStyle2.setBorderRight(BorderStyle.THIN);
for (int i = 0; i < row1.getPhysicalNumberOfCells(); i++) {
row1.getCell(i).setCellStyle(cellStyle2);
}
for (Row row : sheetAt) {
for (Cell cell : row) {
CellStyle cellStyle = cell.getCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cell.setCellStyle(cellStyle);
}
}
FileOutputStream out = new FileOutputStream(projectPath);
excel.write(out);
out.flush();
out.close();
excel.close();
file.close();