如何使用Apache POI为整行应用粗体文本样式?

Kri*_*ary 42 java apache-poi

如何使用Apache POI制作整个excel行单元格粗体文本?

例如:
列标题应为粗体.不是为标题行的每个单元格应用样式,而是如何将一些样式应用于整行?

小智 42

这应该工作正常.

    Workbook wb = new XSSFWorkbook("myWorkbook.xlsx");
    Row row=sheet.getRow(0);
    CellStyle style=null;

    XSSFFont defaultFont= wb.createFont();
    defaultFont.setFontHeightInPoints((short)10);
    defaultFont.setFontName("Arial");
    defaultFont.setColor(IndexedColors.BLACK.getIndex());
    defaultFont.setBold(false);
    defaultFont.setItalic(false);

    XSSFFont font= wb.createFont();
    font.setFontHeightInPoints((short)10);
    font.setFontName("Arial");
    font.setColor(IndexedColors.WHITE.getIndex());
    font.setBold(true);
    font.setItalic(false);

    style=row.getRowStyle();
    style.setFillBackgroundColor(IndexedColors.DARK_BLUE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setFont(font);
Run Code Online (Sandbox Code Playgroud)

如果您不创建defaultFont所有工作簿将默认使用另一个工作簿.

  • `row.getRowStyle()`为我返回null (20认同)
  • 即使创建了列仍然是row.getRowStyle()返回null (3认同)
  • HSSFWorkbook hwb = new HSSFWorkbook(); HSSFSheet sheet = hwb.crateSheet("New Sheet"); HssfRow headRow = sheet.createRow((int)0); CellStyle style = headRow.getRowStyle(); 字体boldFont = hwb.createFont(); boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD); style.setFont(粗体字); //headRow.setRowStyle(style); - >这不起作用Cell cell = headRow.createCell((int)0)cell.setCellStyle(style); - >这个工作我想应用粗体样式来引导行,而不是单个单元格. (2认同)
  • 我打赌你还没有创建你的细胞. (2认同)

小智 15

请在下面找到简单的方法:

XSSFCellStyle style = workbook.createCellStyle();
style.setBorderTop((short) 6); // double lines border
style.setBorderBottom((short) 1); // single line border
XSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 15);
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);                 

Row row = sheet.createRow(0);   
Cell cell0 = row.createCell(0);
cell0.setCellValue("Nav Value");
cell0.setCellStyle(style);  
for(int j = 0; j<=3; j++)
row.getCell(j).setCellStyle(style);
Run Code Online (Sandbox Code Playgroud)

  • 你有没有机会知道为什么row.setRowStyle()不起作用?:/ (3认同)

Adh*_*amo 9

这对我有用

我之前设置了样式的字体并且正常地使用rowheader然后我在循环中设置了在行头的每个单元格上加粗字体的样式.Etvoilà第一排是粗体.

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("FirstSheet");
HSSFRow rowhead = sheet.createRow(0); 
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short)10);
font.setBold(true);
style.setFont(font);
rowhead.createCell(0).setCellValue("ID");
rowhead.createCell(1).setCellValue("First");
rowhead.createCell(2).setCellValue("Second");
rowhead.createCell(3).setCellValue("Third");
for(int j = 0; j<=3; j++)
rowhead.getCell(j).setCellStyle(style);
Run Code Online (Sandbox Code Playgroud)

  • 你有没有机会知道为什么row.setRowStyle()不起作用?:/ (3认同)

omk*_*rra 5

这对我有用

    Object[][] bookData = { { "col1", "col2", 3 }, { "col1", "col2", 3 }, { "col1", "col2", 3 },
            { "col1", "col2", 3 }, { "col1", "col2", 3 }, { "col1", "col2", 3 } };

    String[] headers = new String[] { "HEader 1", "HEader 2", "HEader 3" };

    int noOfColumns = headers.length;
    int rowCount = 0;

    Row rowZero = sheet.createRow(rowCount++);
    CellStyle style = workbook.createCellStyle();
    Font font = workbook.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    style.setFont(font);
    for (int col = 1; col <= noOfColumns; col++) {
        Cell cell = rowZero.createCell(col);
        cell.setCellValue(headers[col - 1]);
        cell.setCellStyle(style);
    }
Run Code Online (Sandbox Code Playgroud)

  • 您有机会知道为什么 row.setRowStyle() 不起作用吗?:/ (2认同)