C# NPOI中的单元格边界实际上是如何工作的?
我现在正在用C#和NPOI创建一些Excel表格。当我为一些细胞设置不同的边界时,我遇到了一些奇怪的行为。 例如,如果我希望第一个单元格(0,0)在右侧有边框,第二个单元格(0,1)在右侧和底部有边框,我写:C# NPOI中的单元格边界实际上是如何工作的?,c#,npoi,C#,Npoi,我现在正在用C#和NPOI创建一些Excel表格。当我为一些细胞设置不同的边界时,我遇到了一些奇怪的行为。 例如,如果我希望第一个单元格(0,0)在右侧有边框,第二个单元格(0,1)在右侧和底部有边框,我写: XSSFWorkbook wb = new XSSFWorkbook(); ISheet sheet = wb.CreateSheet(); IRow row = sheet.CreateRow(0); ICell cell = row.CreateCell(0); ICellStyle
XSSFWorkbook wb = new XSSFWorkbook();
ISheet sheet = wb.CreateSheet();
IRow row = sheet.CreateRow(0);
ICell cell = row.CreateCell(0);
ICellStyle styleRight = wb.CreateCellStyle();
styleRight.BorderRight = BorderStyle.Medium;
cell.CellStyle = styleRight;
row = sheet.CreateRow(1);
cell = row.CreateCell(0);
ICellStyle styleBottomRight = wb.CreateCellStyle();
styleBottomRight.BorderRight = BorderStyle.Medium;
styleBottomRight.BorderBottom = BorderStyle.Medium;
cell.CellStyle = styleBottomRight;
这会弄乱我的结果,并给我的第一个单元格(0,0)一个底部边框
但是,如果切换第二个单元格样式的边框顺序:
row = sheet.CreateRow(1);
cell = row.CreateCell(0);
ICellStyle styleBottomRight = wb.CreateCellStyle();
styleBottomRight.BorderBottom = BorderStyle.Medium;
styleBottomRight.BorderRight = BorderStyle.Medium;
cell.CellStyle = styleBottomRight;
我在第一个单元格上有一个单独的边框,在第二个单元格上有两个边框,从而得到预期的结果
为什么会这样?为什么两个命令的顺序会改变结果?为什么styleBottomRight
会影响第一个单元格的样式设置?
我在.Net Core 3.1中使用的NPOI v2.5.1看起来像是NPOI的一个bug,因为对于第一个案例,生成了两个边界:
<borders count="2">
<border>
<left/>
<right/>
<top/>
<bottom/>
<diagonal/>
</border>
<border>
<left/>
<right style="medium"/>
<top/>
<bottom style="medium"/>
<diagonal/>
</border>
看起来像是NPOI的错误,因为对于第一个案例,生成了两个边界:
<borders count="2">
<border>
<left/>
<right/>
<top/>
<bottom/>
<diagonal/>
</border>
<border>
<left/>
<right style="medium"/>
<top/>
<bottom style="medium"/>
<diagonal/>
</border>
XSSFWorkbook wb = new XSSFWorkbook();
ISheet sheet = wb.CreateSheet();
IRow row = sheet.CreateRow(0);
ICell cell = row.CreateCell(0);
ICellStyle styleRight = wb.CreateCellStyle();
styleRight.BorderBottom = BorderStyle.None;
styleRight.BorderRight = BorderStyle.Medium;
cell.CellStyle = styleRight;
row = sheet.CreateRow(2);
cell = row.CreateCell(0);
ICellStyle styleBottomRight = wb.CreateCellStyle();
styleBottomRight.BorderBottom = BorderStyle.Medium;
styleBottomRight.BorderRight = BorderStyle.Medium;
cell.CellStyle = styleBottomRight;