我没有在互联网上的任何地方找到这些信息,所以我想在这里发帖,因为我自己找到了答案。
我想在电子表格中使用NPOI创建具有边框的单元格,并且如何做到这一点并不明显。
答案 0 :(得分:6)
以下是一些示例代码,用于创建要应用于单元格的单元格样式,以便它们具有边框。
// 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;
答案 1 :(得分:2)
对于XSSF文件。采取这种方法
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);