我想在电子表格粗体中设置标题行的字体。我能够在我的main函数中使用以下代码执行此操作:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet dataSheet = workbook.createSheet("Data");
HSSFCellStyle fontStyle = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setBold(true);
fontStyle.setFont(font);
Row row = dataSheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellStyle(fontStyle);
cell.setCellValue("ID");
由于createCellStyle是HSSFWorkbook的方法,如果我通过调用一个以工作表而不是工作簿作为参数的函数来写入工作表,我该如何设置单元格样式?
public class SummaryXlsCreator {
public static void main(String[] args) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet dataSheet = workbook.createSheet("Data");
writeCDMarker(dataSheet);
}
public static void writeCDMarker(HSSFSheet sheet) {
int rownum = 0;
int cellnum = 0;
Row row = sheet.createRow(rownum++);
Cell cell = row.createCell(cellnum++);
// write first row of sheet "Data"
cell.setCellValue("ID");
}
答案 0 :(得分:2)
使用getWorkbook()获取父级并继续使用现有代码。
sheet.getWorkbbok().createCellStyle();
https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFSheet.html
答案 1 :(得分:0)
您可以使用静态CellUtil.setCellStyleProperties(Cell cell, Map<String, Object> properties)
设置单元格的格式,而无需引用工作簿或工作表。
用法示例:
// Format cell into date time format with "m/d/yy h:mm",
// which is converted to user's locale in Excel.
CellUtil.setCellStyleProperties(cell, Collections.singletonMap(CellUtil.DATA_FORMAT, (short) 22));