2017-09-27 6 views

Répondre

0

Voici un exemple de code pour créer un style de cellule à appliquer aux cellules afin qu'elles aient une bordure.

// create workbook, sheet and a row 
HSSFWorkbook wb = new HSSFWorkbook(); 
var sheet = wb.CreateSheet("Sheet1"); 
var row = sheet.CreateRow(0); 

// create font style 
HSSFFont myFont = (HSSFFont)wb.CreateFont(); 
myFont.FontHeightInPoints = (short)11; 
myFont.FontName = "Tahoma"; 

// create bordered cell style 
HSSFCellStyle borderedCellStyle = (HSSFCellStyle)wb.CreateCellStyle(); 
borderedCellStyle.SetFont(myFont); 
borderedCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Medium; 
borderedCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Medium; 
borderedCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Medium; 
borderedCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Medium; 

// create standard cell style 
HSSFCellStyle standardCellStyle = (HSSFCellStyle)wb.CreateCellStyle(); 
standardCellStyle.SetFont(myFont); 

// create cell and apply bordered style 
var cell = row.CreateCell(0); 
cell.SetCellValue("I have a border"); 
cell.CellStyle = borderedCellStyle; 

// create cell and apply standard stlye 
var cell = row.CreateCell(1); 
cell.SetCellValue("I have NO border"); 
cell.CellStyle = standardCellStyle; 
0

Pour le fichier XSSF. Adoptez cette approche

XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("Demo"); 
XSSFCellStyle defaultStyle = (XSSFCellStyle)workbook.CreateCellStyle(); 
defaultStyle.WrapText = true; 
defaultStyle.Alignment = HorizontalAlignment.Left; 
defaultStyle.VerticalAlignment = VerticalAlignment.Top; 
defaultStyle.BorderBottom = BorderStyle.Thin; 
defaultStyle.BorderTop = BorderStyle.Thin; 
defaultStyle.BorderLeft = BorderStyle.Thin; 
defaultStyle.BorderRight = BorderStyle.Thin; 

sheet.SetColumnWidth(colIndex, 100); 
sheet.SetDefaultColumnStyle(colIndex, defaultStyle);