NPOI设置样式示例

HSSFWorkbook workbook = new HSSFWorkbook();
            MemoryStream ms = new MemoryStream();
            ISheet sheet = workbook.CreateSheet();
            IRow headerRow = sheet.CreateRow(0);

            HSSFCellStyle HeaderCellStyle = (HSSFCellStyle)workbook.CreateCellStyle();
            HeaderCellStyle.BorderTop = BorderStyle.Thin;
            HeaderCellStyle.BorderLeft = BorderStyle.Thin;
            HeaderCellStyle.BorderRight = BorderStyle.Thin;
            HeaderCellStyle.BorderBottom = BorderStyle.Thin;
            HeaderCellStyle.Alignment = HorizontalAlignment.Center;

        //使用自带背景颜色
//HeaderCellStyle.FillForegroundColor = HSSFColor.Lavender.Index; //HeaderCellStyle.FillPattern = FillPattern.SolidForeground; IFont font = workbook.CreateFont(); font.FontHeightInPoints = 9;//9号字体 font.Boldweight = (short)FontBoldWeight.Bold; font.Color = NPOI.HSSF.Util.HSSFColor.Black.Index; HSSFPalette palette = ((HSSFWorkbook)workbook).GetCustomPalette(); palette.SetColorAtIndex(HSSFColor.Lime.Index, (byte)204, (byte)204, (byte)255);//使用自定义背景颜色 HeaderCellStyle.FillForegroundColor = HSSFColor.Lime.Index; HeaderCellStyle.FillPattern = FillPattern.SolidForeground; HeaderCellStyle.SetFont(font); // handling header. foreach (DataColumn column in SourceTable.Columns) { ICell cell = headerRow.CreateCell(column.Ordinal); cell.CellStyle = HeaderCellStyle; cell.SetCellValue(column.ColumnName); }

 

posted @ 2023-11-02 12:58  Shapley  阅读(80)  评论(0编辑  收藏  举报