用poi设置 excel单元格字体加粗
时间: 2023-09-02 17:10:01 浏览: 168
要在Excel单元格中使用POI设置字体加粗,您可以使用HSSFCellStyle类中的setFont方法。下面是一个简单的示例代码,演示如何使用HSSFCellStyle类的setFont方法来设置单元格字体加粗:
```java
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
public class BoldFontExample {
public static void main(String[] args) throws Exception {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Example text");
// Create a new font and alter it.
Font font = workbook.createFont();
font.setBold(true);
// Set the font into a new cell style.
CellStyle style = workbook.createCellStyle();
style.setFont(font);
// Set the cell style
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
workbook.write(fileOut);
fileOut.close();
}
}
```
在这个例子中,我们创建了一个名为workbook的新HSSFWorkbook对象,并创建了一个名为Sheet1的工作表。然后我们创建了一个名为Example text的单元格,并用setFont方法将字体加粗。最后,我们将单元格样式设置为新的单元格样式,并将工作簿写入磁盘文件。
您可以根据需要修改单元格样式对象中的字体属性,例如字体名称,字体大小,字体颜色等,以满足您的需求。
阅读全文
相关推荐


















