POI 设置字体多种颜色

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.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//学习地址:http://www.yiibai.com/apache_poi/apache_poi_cells.html
//http://www.ithao123.cn/content-9901497.html
//SXSSFWorkbook CellStyle 如何更改背景色:
//http://blog.csdn.net/downkang/article/details/14164811
public class TestColor {
	public static void main(String[] args) throws Exception {    
	      XSSFWorkbook workbook = new XSSFWorkbook(); 
	      XSSFSheet spreadsheet = workbook.createSheet("Fontstyle");
	      
	      //数字2的意思是第2行
	      XSSFRow row = spreadsheet.createRow(2);
	      
	      //设置字体
	      XSSFFont font = workbook.createFont();
	      font.setFontHeightInPoints((short) 30);
	      font.setFontName("IMPACT");
	      font.setItalic(true);
	      font.setColor(HSSFColor.GREEN.index);

	      //设置样式
	      XSSFCellStyle style = workbook.createCellStyle();
	      style.setFont(font);

	      //5的意思是第5列
	      XSSFCell cell = row.createCell(5);

	      XSSFRichTextString richString = new XSSFRichTextString( "asdfxghijkadfasdfad" );  
	      //对"Hello,"设置redFont字体  
	      richString.applyFont( 4, 8, font );  
	      //设置单元格中的数据
	      cell.setCellValue(richString);
	      //将样式给了单元格
	      cell.setCellStyle(style);
	      
	      //输出文件
	      FileOutputStream out = new FileOutputStream(new File("fontstyle.xlsx"));
	      workbook.write(out);
	      
	      //关闭流文件
	      workbook.close();
	      out.close();
	      System.out.println("Excel生成成功,点击工程目录中查看...");
	}
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值