java - 使用 Apache POI 将整行加粗

标签 java apache-poi

我正在使用 Apache POI 的 HSSFWorkbook 将数据写入 Excel 电子表格。

我想将整行加粗。有人可以建议怎么做吗?

最佳答案

这样的东西可以与您拥有的东西一起使用吗:

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
    }
}

它基本上遍历传入行中的每个单元格,将样式设置为粗体。应将整行设置为所需的样式。

祝你好运!

编辑

一个更完整的例子:

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
    }
}

这是在第 1 行数据的 xlsx 文件上测试的,生成的文件之后有粗体数据。

关于java - 使用 Apache POI 将整行加粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25437431/

相关文章:

Java Math.toRadians(angle) 与硬计算

java - eclipse中Hudson的配置

Java:如何合并对象

html - 如何在 XHTML 页面中显示 Excel 单元格内容及其样式?

java - Apache POI : Force celldata type to be NO formula?

java - 在apache poi中使用for循环

java - 在 Java 中随机化整数数组的最快方法

Java - 为构造函数的每个实例定义不同的变量?

java - 如何通过apache poi在word中设置表格行的每列不同的高度

java - 如何在 java 中读取 Excel (.xlsx) 文件?