Java – use Apache POI to thicken the entire line

I'm using Apache POI's HSSF workbook to write data into an Excel spreadsheet

I want a whole line bold Can anyone suggest what to do?

Solution

Things like this can use what you have:

public static void makeRowBold(Workbook wb,Row row){
    CellStyle style = wb.createCellStyle();//Create style
    Font font = wb.createFont();//Create font
    font.setBold(true);//Make font bold
    style.setFont(font);//set it to bold

    for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row 
        row.getCell(i).setCellStyle(style);//Set the style
    }
}

It basically traverses each cell in the incoming row and sets the style to bold Should cause the entire line to be set to the desired style

Good luck!

edit

A more complete example:

public static void main(String[] args) {
    Path myFile = Paths.get(System.getProperty("user.home"),"Desktop","tester.xlsx");

        try {
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(myFile.toFile()));
            XSSFSheet sheet = wb.getSheetAt(0);
            makeRowBold(wb,sheet.getRow(0));

            wb.write(new FileOutputStream(myFile.toFile()));
        } catch (IOException e) {
            e.printStackTrace();
        }
}


public static void makeRowBold(Workbook wb,Row row){
    CellStyle style = wb.createCellStyle();//Create style
    Font font = wb.createFont();//Create font
    font.setBold(true);//Make font bold
    style.setFont(font);//set it to bold

    for(int i = 0; i < row.getLastCellNum(); i++){//For each cell in the row 
        row.getCell(i).setCellStyle(style);//Set the sty;e
    }
}

This is tested on the xlsx file. The data is in line 1, and there is bold data after the result file

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>