【小5聊】C#版本NPOI之基础样式设置

NPOI插件,C#版本,整理常见的样式设置

样式设置温馨提示,workbook.CreateCellStyle();只需要定义一次即可,可认为是全局样式设置

具体行和列单元格样式设置,到具体属性值设置即可

curretnCell.CellStyle.Alignment

rowTitle.RowStyle.VerticalAlignment

 1、单元格内换行

  • 效果

  • 代码如下
XSSFWorkbook workbook = new XSSFWorkbook(); //创建一个工作簿
ICellStyle cellStyle = workbook.CreateCellStyle();
cellStyle.WrapText = true;//开启换行

//创建第一行
IRow row = sheet.CreateRow(0);

//给第一行第一列赋值
row.CreateCell(0, CellType.String).SetCellValue("收纳\n化妆品\n美妆\n声控\n沉浸式"); //标签


//给第一列设置样式
row.GetCell(0).CellStyle = cellStyle;

2、高宽度自适应

只能说是相对自适应,根据项目业务进行相应增减

Height:值的单位是:1/20个点,所以要想得到一个点的话,需要乘以20。 HeightInPoints:单位是点,可以不用乘。

SetColumnWidth:第二个参数要乘以256,因为这个参数的单位是1/256个字符宽度,所以要乘以256才是一整个字符宽度

代码

//设置自适应 - 宽度 - 循环列
for (int columnNum = 0; columnNum <= 6; columnNum++)
{
    int columnWidth = sheet.GetColumnWidth(columnNum) / 256;
    for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++)
    {
        IRow currentRow = sheet.GetRow(rowNum);
        if (currentRow.GetCell(columnNum) != null)
        {
            ICell currentCell = currentRow.GetCell(columnNum);
            int length = Encoding.Default.GetBytes(currentCell.ToString()).Length;
            if (columnWidth < length)
            {
                columnWidth = length;
            }
        }
    }

    if (columnWidth > 100) columnWidth = 100;
    if (columnNum == 4) columnWidth = 30;

    sheet.SetColumnWidth(columnNum, columnWidth * 256);
}

//设置自适应 - 高度 - 循环行指定列
for (int rowNum = 1; rowNum <= videoList.Count; rowNum++)
{
    IRow currentRow = sheet.GetRow(rowNum);
    ICell currentCell = currentRow.GetCell(4); //标签
    int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;
    currentRow.HeightInPoints = 20 * (length / 30 + 1);
}

3、列居中显示

4、合并单元格

1)合并行,比如:第一行的第一列和第六列合并

第一行就是:从下标0开始,到下标1结束

第一列到第六列:从下标0开始,到下标5结束

IRow baseInfo = sheet.CreateRow(0);
baseInfo.Sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 1, 0, 5));

2)合并格,比如:第六列的第一行到第六行合并

IRow baseInfo = sheet.CreateRow(0);
baseInfo.Sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 5, 5, 5));

5、单元格填充

  • 效果 

  •  代码

// 初始化对象
HSSFWorkbook book = new HSSFWorkbook();

//先创建好样式
ICellStyle cellStyleBgColor = workbook.CreateCellStyle();    // 创建单元格样式
cellStyleBgColor.FillForegroundColor = IndexedColors.Grey25Percent.Index;   // 选择填充颜色
cellStyleBgColor.FillPattern = FillPattern.SolidForeground;   // 填充方式

//再给单元格赋值样式
IRow rowTitle = sheet.CreateRow(5);//创建表头行
ICell cell = rowTitle.GetCell(indexCell);
cell.CellStyle = cellStyleBgColor;	

6、单元格水平垂直居中

// 创建单元格样式
ICellStyle cellStyle = book.CreateCellStyle();

// 水平和垂直居中
cellStyle.Alignment = HorizontalAlignment.Center;       // 水平居中
cellStyle.VerticalAlignment = VerticalAlignment.Center; // 垂直居中

7、文本字体样式

特别要注意全局字体样式和指定单元格样式

  • 效果

  • 正确设置方式
// 初始化对象
HSSFWorkbook workbook = new HSSFWorkbook();

// 标签名
ISheet iSheet = workbook.CreateSheet("我的用户");

// 字体设置
IFont timeFont = workbook.CreateFont();
timeFont.FontHeightInPoints = 16;
timeFont.IsBold = true;
timeFont.FontName = "微软雅黑";

// 创建单元格样式
ICellStyle timeCsellStyle = workbook.CreateCellStyle();
timeCsellStyle.Alignment = HorizontalAlignment.Center;       // 水平居中
timeCsellStyle.VerticalAlignment = VerticalAlignment.Center; // 垂直居中
timeCsellStyle.SetFont(timeFont);

// 时间行【第一行】
IRow r1 = iSheet.CreateRow(0);
r1.Sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 4));     // 第1行到第1行合并,第1列到第5列合并
r1.HeightInPoints = 30;
r1.CreateCell(0).SetCellValue(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
ICell timeCell = r1.GetCell(0);
timeCell.CellStyle = timeCsellStyle;

。。。未完待续

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈小5

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值