using System; using System.Text; using System.IO; using NPOI.HSSF.UserModel; using NPOI.HSSF.UserModel.Contrib; using NPOI.HPSF; using NPOI.POIFS.FileSystem; using NPOI.HSSF.Util; using NPOI.SS.UserModel; namespace ApplyFontInXls { class Program { static void Main(string[] args) { InitializeWorkbook(); Sheet sheet1=hssfworkbook.CreateSheet("Sheet1"); //font style1: underlined, italic, red color, fontsize=20 Font font1 = hssfworkbook.CreateFont(); font1.Color = HSSFColor.RED.index; font1.IsItalic = true; font1.Underline = (byte)FontUnderlineType.DOUBLE; font1.FontHeightInPoints = 20; //bind font with style 1 CellStyle style1 = hssfworkbook.CreateCellStyle(); style1.SetFont(font1); //font style2: strikeout line, green color, fontsize=15, fontname='宋体' Font font2 = hssfworkbook.CreateFont(); font2.Color = HSSFColor.OLIVE_GREEN.index; font2.IsStrikeout=true; font2.FontHeightInPoints = 15; font2.FontName = "宋体"; //bind font with style 2 CellStyle style2 = hssfworkbook.CreateCellStyle(); style2.SetFont(font2); //apply font styles Cell cell1 = HSSFCellUtil.CreateCell(sheet1.CreateRow(1), 1, "Hello World!"); cell1.CellStyle = style1; Cell cell2 = HSSFCellUtil.CreateCell(sheet1.CreateRow(3), 1, "早上好!"); cell2.CellStyle = style2; //cell with rich text Cell cell3 = sheet1.CreateRow(5).CreateCell(1); HSSFRichTextString richtext = new HSSFRichTextString("Microsoft OfficeTM"); //apply font to "Microsoft Office" Font font4 = hssfworkbook.CreateFont(); font4.FontHeightInPoints = 12; richtext.ApplyFont(0, 16, font4); //apply font to "TM" Font font3=hssfworkbook.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); WriteToFile(); } static HSSFWorkbook hssfworkbook; static void WriteToFile() { //Write the stream data of workbook to the root directory FileStream file = new FileStream(@"test.xls", FileMode.Create); hssfworkbook.Write(file); file.Close(); } static void InitializeWorkbook() { hssfworkbook = new HSSFWorkbook(); //create a entry of DocumentSummaryInformation DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "NPOI Team"; hssfworkbook.DocumentSummaryInformation = dsi; //create a entry of SummaryInformation SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Subject = "NPOI SDK Example"; hssfworkbook.SummaryInformation = si; } } }