NPOI 2 ApplyFontInXlsx

using System;
using System.IO;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.Util;

namespace XSSF
{
	public class ApplyFontInXlsx
	{
		public static void DoAction ()
		{
			IWorkbook workbook = new XSSFWorkbook();
			ISheet sheet1 = workbook.CreateSheet("Sheet1");

			//字体1:下划线,斜体,红色,大小20
			IFont font1 = workbook.CreateFont ();
			font1.Color = HSSFColor.RED.index;
			font1.IsItalic = true;
			font1.Underline = (byte)FontUnderlineType.DOUBLE;
			font1.FontHeightInPoints = 20;

			//将字体绑定到样式
			ICellStyle style1 = workbook.CreateCellStyle ();
			style1.SetFont (font1);

			//字体2:带删除线,绿色,大小15,宋体
			IFont font2 = workbook.CreateFont ();
			font2.Color = HSSFColor.OLIVE_GREEN.index;
			font2.IsStrikeout = true;
			font2.FontHeightInPoints = 15;
			font2.FontName = "宋体";

			ICellStyle style2 = workbook.CreateCellStyle ();
			style2.SetFont (font2);

			//应用样式
			ICell cell1 = sheet1.CreateRow (1).CreateCell (1);
			cell1.SetCellValue ("Hello, World !");
			cell1.CellStyle = style1;

			ICell cell2 = sheet1.CreateRow (3).CreateCell (1);
			cell2.SetCellValue ("Hello, World !");
			cell2.CellStyle = style2;

			//富文本单元格
			ICell cell3 = sheet1.CreateRow (5).CreateCell (1);
			XSSFRichTextString richText = new XSSFRichTextString ("Microsoft OfficeTM");

			//apply font to "Microsoft Office"
			IFont font4 = workbook.CreateFont();
			font4.FontHeightInPoints = 12;
			richText.ApplyFont(0, 16, font4);
			//apply font to "TM"
			IFont font3 = workbook.CreateFont();
			font3.TypeOffset = (short)FontSuperScript.SUPER;
			font3.IsItalic = true;
			font3.Color = HSSFColor.BLUE.index;
			font3.FontHeightInPoints = 8;
			richText.ApplyFont(16, 18, font3);
			
			cell3.SetCellValue(richText);

			FileStream sw = File.Create("ApplyFontInXlsx.xlsx");
			workbook.Write(sw);
			sw.Close();
		}
	}
}

点击查看原图

知识共享许可协议
《NPOI 2 ApplyFontInXlsx》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     162.62ms