セルの文字位置、文字色、セルの色、セルの枠線、などの属性を設定する方法を記載します。
セル内の文字の表示位置を設定する
Dim cell As ICell = sheet1.CreateRow(1).CreateCell(1)
cell.SetCellValue("Hello World")
Dim style1 As ICellStyle = book.CreateCellStyle()
style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right '水平方向
style1.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center '垂直方向
style1.WrapText = True
cell.CellStyle = style1
セルの枠線を設定する
Dim cell As ICell = sheet1.CreateRow(1).CreateCell(1)
Dim style1 As ICellStyle = book.CreateCellStyle()
style1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin '下線
style1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin '左線
style1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin '右線
style1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin '上線
cell.CellStyle = style1
枠に色を付ける
Dim cell As ICell = sheet1.CreateRow(1).CreateCell(1)
Dim style1 As ICellStyle = book.CreateCellStyle()
style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index
style1.FillPattern = FillPattern.SolidForeground
style1.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index
cell.CellStyle = style1
文字属性の設定
Dim cell As ICell = sheet1.CreateRow(1).CreateCell(1)
Dim style1 As ICellStyle = book.CreateCellStyle()
Dim font1 As IFont = book.CreateFont()
font1.FontName = "MS P明朝"
font1.Color = HSSFColor.Red.Index '文字色
font1.Boldweight = FontBoldWeight.Normal
font1.FontHeight = 25
font1.Boldweight = True
style1.SetFont(font1)
cell.CellStyle = style1