setBoldweight不起作用

Naa*_*aaN 1 java xlsx apache-poi

我试图在excel表中添加一些样式,我使用apache poi生成.这是代码,我试图在BOLD中创建第一行.

    SXSSFWorkbook wb=new SXSSFWorkbook();
    Sheet sheet = wb.createSheet("Excelx");

    Row row= sheet.createRow((short)0);
    row.createCell((short) 0).setCellValue("SRNum");
    row.createCell((short) 1).setCellValue("Name");         
    CellStyle cs = wb.createCellStyle();
    Font f = wb.createFont();
    f.setFontHeightInPoints((short) 20);
    f.setBoldweight(Font.BOLDWEIGHT_BOLD);
    cs.setFont(f);
    row.setRowStyle(cs);
Run Code Online (Sandbox Code Playgroud)

但是,第一行仍未改为BOLD.

Naa*_*aaN 7

根据Gagravarr的宝贵评论,我更改了代码:

         Row rowhead= sheet.createRow((short)0);
         Font f = wb.createFont();
         f.setBoldweight(Font.BOLDWEIGHT_BOLD);
         CellStyle cs = wb.createCellStyle();
         cs.setFont(f);

         Cell cell;             
         cell = rowhead.createCell((short) 0);
         cell.setCellValue("SRNum");
         cell.setCellStyle(cs);

         cell = rowhead.createCell((short) 1);
         cell.setCellValue("Name");
         cell.setCellStyle(cs);
Run Code Online (Sandbox Code Playgroud)

  • 请注意,“Font.setBoldweight”在 3.15 中已弃用,并在 3.17 中删除。您应该使用 `Font.setBold` 来代替。 (2认同)