Apache POI(4):字体(font)

本文详细介绍了如何使用Apache POI库来操作Excel文件中的字体样式,包括设置字体颜色、大小、粗细、斜体等,以实现个性化文档格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.hthk.iisz.util;

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class FontTest {

	public static void main(String[] args) throws Exception {
		// fontStyle();
		textDirection();
	}

	// 字体和字体样式
	public static void fontStyle() throws Exception {
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet spreadsheet = workbook.createSheet("Fontstyle");
		XSSFRow row = spreadsheet.createRow(2);
		// create a new font and alter it
		XSSFFont font = workbook.createFont();
		font.setFontHeightInPoints((short) 30);
		font.setFontName("IMPACT");
		font.setItalic(true);
		font.setColor(HSSFColor.BRIGHT_GREEN.index);
		// set font into style
		XSSFCellStyle style = workbook.createCellStyle();
		style.setFont(font);
		// create a cell with a value and set style to it
		XSSFCell cell = row.createCell(1);
		cell.setCellValue("Font Style");
		cell.setCellStyle(style);
		FileOutputStream fos = new FileOutputStream(new File("fontstyle.xlsx"));
		workbook.write(fos);
		fos.close();
		System.out.println("fontstyle.xlsx written successfully");
	}

	// 文字方向
	public static void textDirection() throws Exception {
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet spreadsheet = workbook.createSheet("Text direction");
		XSSFRow row = spreadsheet.createRow(2);
		XSSFCellStyle myStyle = workbook.createCellStyle();
		myStyle.setRotation((short) 0);
		XSSFCell cell = row.createCell(1);
		cell.setCellValue("0D angle");
		cell.setCellStyle(myStyle);
		// 30 degrees
		myStyle = workbook.createCellStyle();
		myStyle.setRotation((short) 30);
		cell = row.createCell(3);
		cell.setCellValue("30D angle");
		cell.setCellStyle(myStyle);
		// 90 degrees
		myStyle = workbook.createCellStyle();
		myStyle.setRotation((short) 90);
		cell = row.createCell(5);
		cell.setCellValue("90D angle");
		cell.setCellStyle(myStyle);
		// 120 degrees
		myStyle = workbook.createCellStyle();
		myStyle.setRotation((short) 120);
		cell = row.createCell(7);
		cell.setCellValue("120D angle");
		cell.setCellStyle(myStyle);
		// 270 degrees
		myStyle = workbook.createCellStyle();
		myStyle.setRotation((short) 270);
		cell = row.createCell(9);
		cell.setCellValue("270D angle");
		cell.setCellStyle(myStyle);
		// 360 degrees
		myStyle = workbook.createCellStyle();
		myStyle.setRotation((short) 360);
		cell = row.createCell(12);
		cell.setCellValue("360D angle");
		cell.setCellStyle(myStyle);
		FileOutputStream out = new FileOutputStream(new File(
				"textdirection.xlsx"));
		workbook.write(out);
		out.close();
		System.out.println("textdirection.xlsx written successfully");
	}

}

效果截图


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值