美文网首页
NPOI边框设置

NPOI边框设置

作者: 腿毛裤 | 来源:发表于2017-11-13 10:02 被阅读0次

设置文档默认为无边框

ISheet sheet = book.CreateSheet(sheetName[i]);

sheet.DisplayGridlines = false;//设置默认为无边框

var head = sheet.CreateRow(0);

for (int a = 0; a < header.Count(); a++)

{

ICell cell = head.CreateCell(a);

XSSFCellStyle fCellStyle = (XSSFCellStyle)book.CreateCellStyle();

XSSFFont ffont = (XSSFFont)book.CreateFont();

ffont.FontHeight = 20 * 20;

ffont.FontHeightInPoints = (short)9.75;//字号

ffont.FontName = "Times New Roman";//字体

ffont.Color = HSSFColor.White.Index;//字色

fCellStyle.SetFont(ffont);

fCellStyle.FillPattern = FillPattern.SolidForeground;//添加背景色必须加这句

fCellStyle.FillForegroundColor = HSSFColor.Grey50Percent.Index;//设置背景填充色50%的灰色

fCellStyle.VerticalAlignment = VerticalAlignment.Center;//垂直对齐

fCellStyle.Alignment = HorizontalAlignment.Center;//水平对齐

fCellStyle.BorderBottom = BorderStyle.Thin;//下边框为细线边框

fCellStyle.BorderLeft = BorderStyle.Thin;//左边框

fCellStyle.BorderRight = BorderStyle.Thin;//上边框

fCellStyle.BorderTop = BorderStyle.Thin;//右边框

fCellStyle.BottomBorderColor = HSSFColor.Grey25Percent.Index;//下边框为细线边框

fCellStyle.LeftBorderColor = HSSFColor.Grey25Percent.Index;//左边框

fCellStyle.RightBorderColor = HSSFColor.Grey25Percent.Index;//上边框

fCellStyle.TopBorderColor = HSSFColor.Grey25Percent.Index;//右边框

cell.CellStyle = fCellStyle;

cell.SetCellValue(header[a]);

}

for (int j = 0; j < datas[i].Count(); j++)

{

var row = sheet.CreateRow(j + 1);

if (row == null)

{

row = sheet.CreateRow(j + 1);

}

row.CreateCells(datas[i][j], styleBody);//styleBody

}

for (int columnNum = 0; columnNum < header.Count; columnNum++)

{

int columnWidth = sheet.GetColumnWidth(columnNum) / 256;//获取当前列宽度

for (int rowNum = 0; rowNum < sheet.LastRowNum; rowNum++)//在这一列上循环行

{

IRow currentRow = sheet.GetRow(rowNum);

ICell currentCell = currentRow.GetCell(columnNum);

int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;//获取当前单元格的内容宽度

if (columnWidth < length)

{

columnWidth = length;

}//若当前单元格内容宽度大于列宽,则调整列宽为当前单元格宽度

}

sheet.SetColumnWidth(columnNum, columnWidth * 256);

}


//行扩展方法填充数据设置并单元格样式(数据类型不可为空,否则会报空指针)

public static void CreateCells(this IRow row, object data, ICellStyle style = null)

{

Type t = data.GetType();

int i = 0;

foreach (var Propertie in t.GetProperties())

{

var cell = row.CreateCell(i++);

if (style != null)

{

cell.CellStyle = style;

}

cell.SetCellValue(Convert.ChangeType(Propertie.GetValue(data), Propertie.PropertyType).ToString());

}

}

相关文章

  • NPOI边框设置

    设置文档默认为无边框 ISheet sheet = book.CreateSheet(sheetName[i]);...

  • NPOI 设置单元格边框

    很多表格中都要使用边框,本节将为你重点讲解NPOI中边框的设置和使用。 边框和其他单元格设置一样也是调用ICell...

  • SwiftUI 设置边框、透明度、阴影

    前言 1、设置边框 1.1 设置边框颜色 默认为1的边框 解释 1.2 设置边框颜色、宽度 设置边框颜色、宽度 2...

  • 8.边框相关属性

    设置边框宽度 border: 3px; 设置边框风格 border-style: solid; 设置边框颜色 bo...

  • CSS盒子模型

    边框:环绕在标签周围的边条 设置边框一:-连写格式:同时设置4条边框{border:边框宽度 边框样式 边框颜色;...

  • CSS border(边框)样式写法总结

    border属性:在网页中设置元素的边框样式。可同时设置边框宽度、边框样式、边框颜色。也可以单独设置上边、右边、下...

  • CALayer

    1. 获取View的CALayer 设置边框宽度 设置边框颜色 设置圆角 设置内容 Question: 这样设置完...

  • 2.css盒模型

    1 盒子模型的概念 2.边框属性 3.边框属性—设置边框样式(border-style) 4.边框属性—设置边框样...

  • 边框和背景

    边框 border border-width: 设置边框的宽度。 border-style: 设置边框的样式。 b...

  • 一个像素的边框

    在一个像素边框的创建时,要先设置边框颜色再设置边框宽度 如果先设置宽度再设置颜色,则会显示一个点的边框。 titl...

网友评论

      本文标题:NPOI边框设置

      本文链接:https://www.haomeiwen.com/subject/elvamxtx.html