zoukankan      html  css  js  c++  java
  • 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:UsersAdministratorDesktopaa.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;
                    }


                }
            }
        }

  • 相关阅读:
    echarts 地图 动态 展示 结合css+js
    优化之误!
    SQL Server 运行计划操作符具体解释(3)——计算标量(Compute Scalar)
    NHibernate概括
    C++的IO操作
    (数据结构整理)NJUPT1054
    nginx模块开发
    Html学习(三) 分类学习
    android 屏幕适配
    【POJ 1845】 Sumdiv (整数唯分+约数和公式+二分等比数列前n项和+同余)
  • 原文地址:https://www.cnblogs.com/Asa-Zhu/p/8961874.html
Copyright © 2011-2022 走看看