加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 使用Apache POI使整行变粗

发布时间:2020-12-14 05:41:48 所属栏目:Java 来源:网络整理
导读:我正在使用Apache POI的HSSFWorkbook将数据写入Excel电子表格. 我想整整一行加粗.有人可以建议怎么做吗? 解决方法 像这样的东西可以用你所拥有的东西: public static void makeRowBold(Workbook wb,Row row){ CellStyle style = wb.createCellStyle();//Cr
我正在使用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
    }
}

这是在xlsx文件上测试的,数据在第1行,结果文件后面有粗体数据.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读