本文概述
- 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());
}
}
}
【apache poi excel字体】输出:
文章图片
推荐阅读
- apache poi excel文档标题
- apache poi合并单元
- apache poi excel单元格颜色
- apache poi excel单元格边界
- apache poi excel单元格
- apache poi excel对齐单元格
- apache poi excel日期单元格
- apache poi excel工作簿
- apache poi excel文档处理