个性化阅读
专注于IT技术分析

apache poi excel字体

本文概述

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());
	    }
	}
}

输出:

赞(0)
未经允许不得转载:srcmini » apache poi excel字体

评论 抢沙发

评论前必须登录!