一个问题
用NPOI合并Excel单元格后,其边框如何设置,这个问题困扰了有段时间。因为看不太懂文档,只能在网上搜索。但基本上都是用HSSFWorkbook针对的xls文件,对xlsx文件的则没有。此外还有一些用循环做的,更是舍近求远。
用RegionUtil.SetBorderXXX解决
1、引用
using NPOI.SS.Util;
2、合并单元格
CellRangeAddress region = new CellRangeAddress(1, 2, i, i);
sheet.AddMergedRegion(region);//合并单元格。
3、设置框线
RegionUtil.SetBorderLeft(1, region, sheet, workbook)//左
RegionUtil.SetBorderRight(1, region, sheet, workbook)//右
RegionUtil.SetBorderTop(1, region, sheet, workbook)//顶
RegionUtil.SetBorderBottom(1, region, sheet, workbook)//底
附:
namespace NPOI.SS.Util
{
public class RegionUtil
{
public static void SetBorderBottom(int border, CellRangeAddress region, ISheet sheet, IWorkbook workbook);
public static void SetBorderLeft(int border, CellRangeAddress region, ISheet sheet, IWorkbook workbook);
public static void SetBorderRight(int border, CellRangeAddress region, ISheet sheet, IWorkbook workbook);
public static void SetBorderTop(int border, CellRangeAddress region, ISheet sheet, IWorkbook workbook);
public static void SetBottomBorderColor(int color, CellRangeAddress region, ISheet sheet, IWorkbook workbook);
public static void SetLeftBorderColor(int color, CellRangeAddress region, ISheet sheet, IWorkbook workbook);
public static void SetRightBorderColor(int color, CellRangeAddress region, ISheet sheet, IWorkbook workbook);
public static void SetTopBorderColor(int color, CellRangeAddress region, ISheet sheet, IWorkbook workbook);
}
}