提问人:rickygrimes 提问时间:8/22/2014 最后编辑:Matthias Braunrickygrimes 更新时间:3/12/2021 访问量:36226
使用 Apache POI 将整行加粗
Make the entire row bold using Apache POI
答:
32赞
Levenal
8/22/2014
#1
这样的东西会与你所拥有的一起使用吗:
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 文件上测试的,生成的文件之后有粗体数据。
评论
0赞
rickygrimes
8/22/2014
那只有第一排呢?我希望整行都是粗体,而不是整个 excel :)
1赞
Rory
2/17/2016
我希望能够设置行。RowStyle,而不需要在每个单独的单元格上设置样式。你知道为什么这不起作用吗?
3赞
DanielK
10/4/2016
setBoldweight()
在 3.15 beta 2 中已弃用,并计划在 3.17 中删除。请改用。poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/......setBold(true)
3赞
BullyWiiPlaza
1/8/2017
#2
使用未弃用的 API:
public static void makeBold(Workbook workbook, Row row)
{
CellStyle cellStyle = cell.getCellStyle();
cellStyle.setFont(font);
for (int rowIndex = 0; rowIndex < row.getLastCellNum(); rowIndex++)
{
Cell cell = row.getCell(rowIndex);
XSSFFont font = (XSSFFont) workbook.createFont();
font.setBold(true);
cell.setCellStyle(cellStyle);
}
}
评论
1赞
JRA_TLL
4/19/2017
代替 ,只需使用XSSFFont font = (XSSFFont) workbook.createFont();
final Font font = workbook.createFont();
0赞
BullyWiiPlaza
4/20/2017
@JRA_TLL:不,因为它不再有方法了setBold()
2赞
Marc
7/24/2018
不要为每个单元格创建新样式。工作簿中的样式有限。将样式移到循环之外。
0赞
BullyWiiPlaza
7/24/2018
@Marc:是的,现在应用了更改
0赞
user1438038
1/24/2023
CellStyle/XSSFCellStyle 和 Font/XSSFFont 有什么区别?都看过。
2赞
Shila Mosammami
3/12/2021
#3
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class test {
public static void main(String[] args) throws FileNotFoundException, IOException {
String toCreateFullPath="C:\\Users\\Y[![enter image description here][1]][1]our Desired Path\\testExcel.xlsx";
Path path=Paths.get(toCreateFullPath);
if(!Files.exists(path.getParent())){
try {
Files.createDirectory(path.getParent());
System.out.println("Directory created");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Error Directory is Not Created");
}
}
XSSFWorkbook workbook =new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("testSheet");
String[] columns = {"id", "Name", "Family","Address", "Phone"};
CellStyle hStyle=null;
// Creating a font
XSSFFont font= workbook.createFont();
font.setFontHeightInPoints((short)12);
font.setFontName("Arial");
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
font.setItalic(false);
hStyle=workbook.createCellStyle();
hStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
hStyle.setAlignment(CellStyle.ALIGN_CENTER);
// Setting font to style
hStyle.setFont(font);
// Create a Row
Row headerRow = sheet.createRow(0);
// Create cells
for(int i = 0; i < columns.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(columns[i]);
// Setting cell style
cell.setCellStyle(hStyle);
sheet.autoSizeColumn(i);
}
try (FileOutputStream outputStream = new FileOutputStream(toCreateFullPath)) {
workbook.write(outputStream);
}
}
}
评论