C# 实现Excel单元格画边框

编程入门 行业动态 更新时间:2024-10-28 16:17:14

C# 实现Excel单元格画<a href=https://www.elefans.com/category/jswz/34/1766074.html style=边框"/>

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单元格画边框

本文发布于:2023-07-28 17:59:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269541.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:边框   单元格   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!