当前位置:   article > 正文

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

npoi ifont

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

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

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

curretnCell.CellStyle.Alignment

rowTitle.RowStyle.VerticalAlignment

 1、单元格内换行

  • 效果

  • 代码如下
  1. XSSFWorkbook workbook = new XSSFWorkbook(); //创建一个工作簿
  2. ICellStyle cellStyle = workbook.CreateCellStyle();
  3. cellStyle.WrapText = true;//开启换行
  4. //创建第一行
  5. IRow row = sheet.CreateRow(0);
  6. //给第一行第一列赋值
  7. row.CreateCell(0, CellType.String).SetCellValue("收纳\n化妆品\n美妆\n声控\n沉浸式"); //标签
  8. //给第一列设置样式
  9. row.GetCell(0).CellStyle = cellStyle;

2、高宽度自适应

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

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

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

代码

  1. //设置自适应 - 宽度 - 循环列
  2. for (int columnNum = 0; columnNum <= 6; columnNum++)
  3. {
  4. int columnWidth = sheet.GetColumnWidth(columnNum) / 256;
  5. for (int rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++)
  6. {
  7. IRow currentRow = sheet.GetRow(rowNum);
  8. if (currentRow.GetCell(columnNum) != null)
  9. {
  10. ICell currentCell = currentRow.GetCell(columnNum);
  11. int length = Encoding.Default.GetBytes(currentCell.ToString()).Length;
  12. if (columnWidth < length)
  13. {
  14. columnWidth = length;
  15. }
  16. }
  17. }
  18. if (columnWidth > 100) columnWidth = 100;
  19. if (columnNum == 4) columnWidth = 30;
  20. sheet.SetColumnWidth(columnNum, columnWidth * 256);
  21. }
  22. //设置自适应 - 高度 - 循环行指定列
  23. for (int rowNum = 1; rowNum <= videoList.Count; rowNum++)
  24. {
  25. IRow currentRow = sheet.GetRow(rowNum);
  26. ICell currentCell = currentRow.GetCell(4); //标签
  27. int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;
  28. currentRow.HeightInPoints = 20 * (length / 30 + 1);
  29. }

3、列居中显示

4、合并单元格

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

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

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

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

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

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

5、单元格填充

  • 效果 

  •  代码
  1. // 初始化对象
  2. HSSFWorkbook book = new HSSFWorkbook();
  3. //先创建好样式
  4. ICellStyle cellStyleBgColor = workbook.CreateCellStyle(); // 创建单元格样式
  5. cellStyleBgColor.FillForegroundColor = IndexedColors.Grey25Percent.Index; // 选择填充颜色
  6. cellStyleBgColor.FillPattern = FillPattern.SolidForeground; // 填充方式
  7. //再给单元格赋值样式
  8. IRow rowTitle = sheet.CreateRow(5);//创建表头行
  9. ICell cell = rowTitle.GetCell(indexCell);
  10. cell.CellStyle = cellStyleBgColor;

6、单元格水平垂直居中

  1. // 创建单元格样式
  2. ICellStyle cellStyle = book.CreateCellStyle();
  3. // 水平和垂直居中
  4. cellStyle.Alignment = HorizontalAlignment.Center; // 水平居中
  5. cellStyle.VerticalAlignment = VerticalAlignment.Center; // 垂直居中

7、文本字体样式

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

  • 效果

  • 正确设置方式
  1. // 初始化对象
  2. HSSFWorkbook workbook = new HSSFWorkbook();
  3. // 标签名
  4. ISheet iSheet = workbook.CreateSheet("我的用户");
  5. // 字体设置
  6. IFont timeFont = workbook.CreateFont();
  7. timeFont.FontHeightInPoints = 16;
  8. timeFont.IsBold = true;
  9. timeFont.FontName = "微软雅黑";
  10. // 创建单元格样式
  11. ICellStyle timeCsellStyle = workbook.CreateCellStyle();
  12. timeCsellStyle.Alignment = HorizontalAlignment.Center; // 水平居中
  13. timeCsellStyle.VerticalAlignment = VerticalAlignment.Center; // 垂直居中
  14. timeCsellStyle.SetFont(timeFont);
  15. // 时间行【第一行】
  16. IRow r1 = iSheet.CreateRow(0);
  17. r1.Sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 4)); //1行到第1行合并,第1列到第5列合并
  18. r1.HeightInPoints = 30;
  19. r1.CreateCell(0).SetCellValue(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  20. ICell timeCell = r1.GetCell(0);
  21. timeCell.CellStyle = timeCsellStyle;

。。。未完待续

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/773823
推荐阅读
相关标签
  

闽ICP备14008679号