边框"/>
C# 实现Excel单元格画边框
代码阐释:我这里是走固定的Excel模板,发现导出时一部分有边框的单元格消失了,所以选择用代码一个一个的画单元格,网上有那种全是边框的代码,这里我只是在汇总表这块加边框.记得using相关包…
下面是实现代码及效果图(黑色边框)
1.实例化ICellStyle,单元格格式.
string folder = Guid.NewGuid().ToString();//生成路径if (!Directory.Exists(Application.StartupPath + "\\ExcelBox\\" + folder))//Directory类提供了在目录和子目录中进行创建移动和列举操作的静态方法。判断文件夹ExcelBox中是否存在文件。{Directory.CreateDirectory(Application.StartupPath + "\\ExcelBox\\" + folder);}FileStream fs = new FileStream(Application.StartupPath + "\\xls\\2020MB\\2019年下期奖金发放报告.xls", FileMode.Open, FileAccess.Read);//通过流打开读取数据库IWorkbook workbook = WorkbookFactory.Create(fs);//单元格样式ICellStyle cellStyle = workbook.CreateCellStyle();cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;ICellStyle cellStyle1 = workbook.CreateCellStyle();cellStyle1.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;cellStyle1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;2.创建单元格及格式.
ISheet sheetAt = workbook.GetSheetAt(2);//创建页签for (int i = 13; i <= 15; i++){ICell Cell = sheetAt.GetRow(4).CreateCell(i, CellType.STRING);Cell.CellStyle = cellStyle;}
3.插入值
sheetAt.GetRow(4).GetCell(13).SetCellValue(I1);//应发奖金2
sheetAt.GetRow(4).GetCell(14).SetCellValue(J1);//个所税
sheetAt.GetRow(4).GetCell(15).SetCellValue(I1 - J1);//实发奖金
更多推荐
C# 实现Excel单元格画边框
发布评论