1个回答
test
2024-06-24
jdk1.8
引入POI依赖:
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
public class Main {
public static void main(String[] args) {
try (XSSFWorkbook workbook = new XSSFWorkbook();
OutputStream out = Files.newOutputStream(Paths.get("workbook.xlsx"))) {
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow((short) 0);
Cell cell = row.createCell((short) 0);
cell.setCellValue("TEST---");
// 创建一个单元格样式
XSSFCellStyle style = workbook.createCellStyle();
cell.setCellStyle(style);
// 填充色
style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 字体颜色
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
style.setFont(font);
workbook.write(out);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
实现效果:
参考:
https://blog.csdn.net/lipinganq/article/details/78132021
回复
适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容