java - 如何在 poi apache java 中同时设置单元格 fontHeight 9 和 Bold
问题描述
我正在使用 poi apache libraru 创建文件 Excel。在标题栏中,我想将字体高度设置为 9 并设置字体粗体。但我只能在上面的两件事中设置一件事。这是我的代码。你能帮助我吗。感谢
XSSFCellStyle cellFont_9 = workbook.createCellStyle();
cellFont_9.setFont(font_9);
XSSFCellStyle cellFontBold = workbook.createCellStyle();
cellFontBold.setFont(fontBold);
setRowStyleExcell(workbook, headerRow, cellFont_9);
setRowStyleExcell(workbook, headerRow, cellFontBold);
在这种情况下,我的字体是粗体
其他情况:
XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font_9);
cellStyle.setFont(fontBold);
setRowStyleExcell(workbook, headerRow, cellStyle);
它只是粗体
这是我的 setRowStyleExcell() 函数:
public void setRowStyleExcell(Workbook workbook, Row row, XSSFCellStyle style){
for(int i = 0; i < row.getLastCellNum(); i++){
//For each cell in the row
if(row.getCell(i) != null){
row.getCell(i).setCellStyle(style);//Set the style
}
}
}
解决方案
您必须创建具有这两个属性的对象,Font
然后设置Font
XSSFCellStyle
在你的情况下
Font font = .... // Initialize it the way you do now
font.setBold(true); // Set bold
font.setFontHeight((short) 9); // Set font height
cellFont_9.setFont(font_9); // Set on XSSFCellStyle
推荐阅读
- bash - 尝试在 .bashrc 文件中设置提示颜色,但以 root 身份登录时不起作用
- django - Django:图像字段未显示
- reactjs - 如何在 Firebase(ReactJS / Firebase 实时数据库)中存储和格式化日期?
- java - hsql,JpaRepository,命名参数到字符串
- spring-boot - 当我使用从 REST API 列表中删除时不兼容的类型
- java - java中的流和数组
- python - 查找 Gunicorn 配置文件
- octave - 如何在 Octave 中导出带有索引的矩阵?
- r - R中的日期格式转换
- jquery - 通过第一个 Datepicker 中的 onChange 修改后,第二个 Bootstrap Datepicker 不会自动关闭