C# npoi,设置黑色边框
时间: 2024-10-23 14:07:32 浏览: 74
在C#中使用NPOI设置单元格的黑色边框,你可以通过`BorderStyle`枚举和`Border`对象来完成。以下是在HSSF和EPPlus库中分别设置黑色边框的方式:
### 方式一:HSSF(低级API)
```csharp
using org.apache.poi.hssf.usermodel;
// 创建单元格
HSSFCell cell = row.createCell(cellIndex);
// 设置边框
HSSFCellStyle style = workbook.CreateCellStyle();
style.BorderTop = HSSFCellStyle.BorderStyle.THIN; // 设置顶部边框
style.BorderBottom = HSSFCellStyle.BorderStyle.THIN; // 设置底部边框
style.BorderLeft = HSSFCellStyle.BorderStyle.THIN; // 左侧边框
style.BorderRight = HSSFCellStyle.BorderStyle.THIN; // 右侧边框
style.BorderSide = BorderStyle.BorderSide.All; // 设置所有边框
style.BorderColor = HSSFColor.BLACK.Index; // 设置边框颜色为黑色
cell.CellStyle = style;
```
### 方式二:EPPlus(高级API)
```csharp
using OfficeOpenXml;
// 创建单元格范围
var cellRange = worksheet.Cells[cellAddress];
// 设置边框
cellRange.Border = ExcelBorderStyle.Thin; // 设置所有边框为细线
cellRange.LeftBorder.Color = System.Drawing.Color.Black; // 设置左侧边框为黑色
cellRange.RightBorder.Color = System.Drawing.Color.Black; // 设置右侧边框为黑色
cellRange.TopBorder.Color = System.Drawing.Color.Black; // 设置上部边框为黑色
cellRange.BottomBorder.Color = System.Drawing.Color.Black; // 设置下部边框为黑色
```
### 方式三:XSSF(基于XML的低级API)
```csharp
using NPOI.XSSF用户模型;
// 创建单元格
IXSSFCell cell = (IXSSFCell)row.CreateCell(cellIndex);
// 创建样式并设置边框
IXSSFCellStyle style = (IXSSFCellStyle)workbook.CreateCellStyle();
style.BorderTop = NPOI.SS.UserModel.BorderStyleThin; // 设置顶部边框
style.BorderBottom = NPOI.SS.UserModel.BorderStyleThin; // 设置底部边框
style.BorderLeft = NPOI.SS.UserModel.BorderStyleThin; // 设置左侧边框
style.BorderRight = NPOI.SS.UserModel.BorderStyleThin; // 设置右侧边框
style.BorderBottomColor = NPOIColor.Black; // 设置边框颜色为黑色
cell.CellStyle = style;
```
阅读全文
相关推荐


















