本文概述
Apache POI提供了处理Excel工作表中的字体的方法。我们可以创建字体,设置颜色,设置大小等。字体是提供处理字体的方法的接口。
让我们看一个示例,在该示例中,我们为单元格内容创建和设置新字体。
Apache POI Excel单元字体示例
package poiexample;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class FontExample {
public static void main(String[] args) {
try (OutputStream fileOut = new FileOutputStream("srcmini.xls")) {
Workbook wb = new HSSFWorkbook(); // Creating a workbook
Sheet sheet = wb.createSheet("Sheet"); // Creating a sheet
Row row = sheet.createRow(1); // Creating a row
Cell cell = row.createCell(1); // Creating a cell
CellStyle style = wb.createCellStyle(); // Creating Style
cell.setCellValue("Hello, srcmini!");
// Creating Font and settings
Font font = wb.createFont();
font.setFontHeightInPoints((short)11);
font.setFontName("Courier New");
font.setItalic(true);
font.setStrikeout(true);
// Applying font to the style
style.setFont(font);
// Applying style to the cell
cell.setCellStyle(style);
wb.write(fileOut);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
输出:
评论前必须登录!
注册