使用Apache POI将整个行设为粗体 [英] Make the entire row bold using Apache POI
本文介绍了使用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
}
}
这是在xlsx文件上进行测试的,该文件的数据位于第1行,其后的文件中的数据为粗体.
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.
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 was tested on an xlsx file with data in row 1, the resulting file had bold data afterwards.
这篇关于使用Apache POI将整个行设为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文