NPOI给单元格加范围边框

HSSFWorkbook workbook2 = new HSSFWorkbook();
        //XSSFWorkbook workbook2 = new XSSFWorkbook();//建立Excel2007对象
        HSSFSheet sheet1;
        /// <summary>
        /// 99乘法表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            sheet1 = (HSSFSheet )workbook2.CreateSheet("Sheet1");
            for (int rowIndex = 0; rowIndex < 9; rowIndex++)
            {
                HSSFRow row = (HSSFRow)sheet1.CreateRow(rowIndex);
                for (int colIndex = 0; colIndex <= rowIndex; colIndex++)
                {
                    HSSFCell cell = (HSSFCell)row.CreateCell(colIndex);
                    cell.SetCellValue(String.Format("{0}*{1}={2}", rowIndex + 1, colIndex + 1, (rowIndex + 1) * (colIndex + 1)));
                }
            }
            //加范围边框
            AddRengionBorder(0, 9, 0, 9);
            FileStream fs = new FileStream(@"C:\Users\Administrator\Desktop\aa.xls", FileMode.Create, FileAccess.Write);
            workbook2.Write(fs);
            fs.Dispose();
            MessageBox.Show("导出OK");
        }
       
        


        /// <summary>
        /// 加范围边框
        /// </summary>
        /// <param name="firstRow">起始行</param>
        /// <param name="lastRow">结束行</param>
        /// <param name="firstCell">起始列</param>
        /// <param name="lastCell">结束列</param>
        /// <returns></returns>
        public void AddRengionBorder(int firstRow, int lastRow, int firstCell, int lastCell)
        {
            //HSSFCellStyle Style = (HSSFCellStyle)workbook2.CreateCellStyle();
            for (int i = firstRow; i < lastRow; i++)
            {
                for (int n = firstCell; n < lastCell; n++)
                {
                    ICell cell;
                    cell = sheet1.GetRow(i).GetCell(n);
                    if (cell == null)
                    {
                        cell = sheet1.GetRow(i).CreateCell(n);
                        cell.SetCellValue(" ");
                    }
                    HSSFCellStyle Style = workbook2.CreateCellStyle() as HSSFCellStyle;
                    为首行加上方边框
                    if (i == firstRow)
                    {
                        Style.BorderTop = ss.UserModel.BorderStyle.THIN;
                    }
                    //为末行加下方边框
                    if (i == lastRow-1)
                    {
                        Style.BorderBottom = ss.UserModel.BorderStyle.THIN;
                    }
                    //为首列加左边框
                    if (n == firstCell)
                    {
                        Style.BorderLeft = ss.UserModel.BorderStyle.THIN;
                    }
                    //为末列加右边框
                    if (n == lastCell-1)
                    {
                        Style.BorderRight = ss.UserModel.BorderStyle.THIN;
                    }
                    cell.CellStyle = Style;
                }


            }
        }
    }

转载于:https://www.cnblogs.com/Asa-Zhu/p/8961874.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用NPOI单元格边框,可以使用CellStyle对象中的Border属性来设置边框线的样式和颜色。 下面是一个示例代码,演示如何给单元格边框: ``` using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; // 创建一个新的工作簿 var workbook = new XSSFWorkbook(); // 创建一个新的单元格样式 var cellStyle = workbook.CreateCellStyle(); // 设置边框线的样式和颜色 cellStyle.BorderBottom = BorderStyle.Thin; cellStyle.BorderTop = BorderStyle.Thin; cellStyle.BorderLeft = BorderStyle.Thin; cellStyle.BorderRight = BorderStyle.Thin; cellStyle.BottomBorderColor = IndexedColors.Black.Index; cellStyle.TopBorderColor = IndexedColors.Black.Index; cellStyle.LeftBorderColor = IndexedColors.Black.Index; cellStyle.RightBorderColor = IndexedColors.Black.Index; // 创建一个新的工作表 var sheet = workbook.CreateSheet("Sheet1"); // 创建一个新的行 var row = sheet.CreateRow(0); // 创建一个新的单元格,并设置边框 var cell = row.CreateCell(0); cell.SetCellValue("Hello, World!"); cell.CellStyle = cellStyle; // 保存工作簿 using (var fileStream = new FileStream("example.xlsx", FileMode.Create, FileAccess.Write)) { workbook.Write(fileStream); } ``` 在上面的示例中,我们首先创建了一个新的工作簿,并在其中创建了一个新的单元格样式。然后,我们设置了边框线的样式和颜色,并创建了一个新的工作表和行。最后,我们创建了一个新的单元格,并将边框样式应用于该单元格。最终,我们将工作簿保存到文件中。 注意,这里我们使用了XSSFWorkbook类来创建工作簿和单元格样式。如果你是使用HSSFWorkbook类创建工作簿,则需要使用HSSFCellStyle类来创建单元格样式。其他部分的代码基本相同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值