Java 如何使用ApachePOI为整行应用粗体文本样式?

Java 如何使用ApachePOI为整行应用粗体文本样式?,java,apache-poi,Java,Apache Poi,如何使用ApachePOI将整个excel行单元格设置为粗体文本 例如: 列标题应为粗体。如何将某些样式应用于整行,而不是为标题行的每个单元格应用样式?这应该可以正常工作 Workbook wb = new XSSFWorkbook("myWorkbook.xlsx"); Row row=sheet.getRow(0); CellStyle style=null; XSSFFont defaultFont= wb.createFont(); defaul

如何使用ApachePOI将整个excel行单元格设置为粗体文本

例如:

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

这应该可以正常工作

    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);

如果不创建
defaultFont
,则所有工作簿都将使用另一个作为默认值

这是我的工作

    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);
    }
我在前面设置了样式的字体,并使行标题正常,然后在每个行标题单元格上设置了粗体字体的样式循环。瞧,第一行是粗体的

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);
HSSFWorkbook wb=新的HSSFWorkbook();
HSSFSheet sheet=wb.createSheet(“第一张纸”);
HSSFRow rowhead=sheet.createRow(0);
HSSFCellStyle=wb.createCellStyle();
hssfont font=wb.createFont();
font.setFontName(hssfont.font_ARIAL);
font.setFontHeightInPoints((短)10);
font.setBold(true);
style.setFont(字体);
rowhead.createCell(0.setCellValue(“ID”);
rowhead.createCell(1.setCellValue(“第一”);
rowhead.createCell(2.setCellValue(“第二”);
rowhead.createCell(3.setCellValue(“第三”);

对于(int j=0;j请找到以下简单方法:

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);
XSSFCellStyle=workbook.createCellStyle();
style.setBorderTop((短)6);//双线边框
style.setBorderBottom((短)1);//单行边框
XSSFFont font=workbook.createFont();
font.setFontHeightInPoints((短)15);
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
style.setFont(字体);
Row Row=sheet.createRow(0);
Cell cell0=行。createCell(0);
cell0.setCellValue(“Nav值”);
cell0.setCellStyle(style);

对于(int j=0;j这对我很有效

    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);
    }
Object[]bookData={{“col1”,“col2”,3},{“col1”,“col2”,3},{“col1”,“col2”,3},
{“col1”,“col2”,3},{“col1”,“col2”,3},{“col1”,“col2”,3};
字符串[]头=新字符串[]{“头1”、“头2”、“头3”};
int noOfColumns=headers.length;
int rowCount=0;
rowZero=sheet.createRow(rowCount++);
CellStyle style=workbook.createCellStyle();
Font=workbook.createFont();
font.setBoldweight(font.BOLDWEIGHT\u BOLD);
style.setFont(字体);

对于(int col=1;col一个已完成的简单示例:

package io.github.baijifeilong.excel;

import lombok.SneakyThrows;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

/**
 * Created by BaiJiFeiLong@gmail.com at 2019/12/6 11:41
 */
public class ExcelBoldTextDemo {

    @SneakyThrows
    public static void main(String[] args) {

        new XSSFWorkbook() {{
            XSSFRow row = createSheet().createRow(0);
            row.setRowStyle(createCellStyle());
            row.getRowStyle().getFont().setBold(true);
            row.createCell(0).setCellValue("Alpha");
            row.createCell(1).setCellValue("Beta");
            row.createCell(2).setCellValue("Gamma");
        }}.write(new FileOutputStream("demo.xlsx"));
    }
}

很抱歉,我忘了输入。我正在使用HSSFWorkbook.HSSFWorkbook hwb=new HSSFWorkbook();HSSFSheet sheet=hwb.clatesheet(“新工作表”);HssfRow headRow=sheet.createRow((int)0);CellStyle style=headRow.getRowStyle();Font boldFont=hwb.createFont();boldFont.setBoldweight(Font.BOLDWEIGHT);//headRow.setRowStyle(style);-->Cell=headlow.createCell((int)0)Cell.setCellStyle(style);-->这不起作用我想将粗体样式应用于整个行,而不是单个单元格。
row.getRowStyle()
即使列仍然是创建的,也会为Meen返回null。getRowStyle()返回null这也很有帮助:此链接可能会帮助您。您是否知道row.setRowStyle()不工作的原因?:/you是否知道row.setRowStyle()不工作的原因?:/you是否知道row.setRowStyle()不工作的原因不起作用?:/No Erdal。如果它起作用,我将只逐行执行,而不是逐列执行。@ErdalG.row.setRowStyle如果在行的单元格中设置值后进行设置,则可以工作。如果在上面的回答中,setRowStyle在for循环之后完成,则可以工作!使用Poi 3.9版本进行测试。@ErdalG.Sorry。我只是部分正确。它可以工作使用LibreOffice而不是MS Excel!可能需要使用最新的Excel。什么是
createCellStyle()