使用 Apache POI 将整行加粗 [英] Make the entire row bold using Apache POI
本文介绍了使用 Apache POI 将整行加粗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Apache POI 的 HSSFWorkbook 将数据写入 Excel 电子表格.
I am using Apache POI's HSSFWorkbook to write data to Excel spreadsheets.
我想让整行加粗.有人可以建议怎么做吗?
I want to make an entire row bold. Can someone please suggest how to do it?
推荐答案
这样的事情是否适用于您现有的:
Would something like this work with 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 goes over each cell in the row passed in, setting the style to a bold one. Should result in the whole row being set to the desired 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 文件上测试的,结果文件之后有粗体数据.
This was tested on an xlsx file with data in row 1, the resulting file had bold data afterwards.
这篇关于使用 Apache POI 将整行加粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文