用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法

本篇文章小编为大家介绍,用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法。需要的朋友参考下

今天在做项目中,遇到使用代码生成具有一定样式的Excel,找了很多资料,最后终于解决了,Excel中格式的设置,以及单元格的合并等等。下面就介绍下,使用NPOI类库操作Excel的方法。

  1.首先我们先在内存中生成一个Excel文件,代码如下:

    HSSFWorkbook book = new HSSFWorkbook();
        ISheet sheet = book.CreateSheet("Sheet1");

  2.然后在新创建的sheet里面,创建我们的行和列,代码如下:

复制代码 代码如下:

IRow row = sheet.CreateRow(index);//index代表多少行
row.HeightInPoints = 35;//行高
ICell cell = row.CreateCell(0);//创建第一列
cell.SetCellValue(“设置单元格的值”);

  3.设置单元格的样式已经字体大小,边框,以及合并单元格

  (1).创建单元格字体的样式及大小

复制代码 代码如下:

///
        /// 获取字体样式
        ///

        /// Excel操作类
        /// 字体名
        /// 字体颜色
        /// 字体大小
        ///
        public static IFont GetFontStyle(HSSFWorkbook hssfworkbook, string fontfamily, HSSFColor fontcolor, int fontsize)
        {
            IFont font1 = hssfworkbook.CreateFont();
            if (string.IsNullOrEmpty(fontfamily))
            {
                font1.FontName = fontfamily;
            }
            if (fontcolor != null)
            {
                font1.Color = fontcolor.GetIndex();
            }
            font1.IsItalic = true;
            font1.FontHeightInPoints = (short)fontsize;
            return font1;
        }
  
  (2).设置单元格内显示数据的格式
复制代码 代码如下:

ICell cell = row.CreateCell(1);
ICellStyle cellStyleNum = Excel.GetICellStyle(book);
IDataFormat formatNum = book.CreateDataFormat();
cellStyleNum.DataFormat = formatNum.GetFormat("0.00E+00");//设置单元格的格式为科学计数法cell.CellStyle = cellStyleNum;

  (3).创建单元格的边框,背景颜色,以及对齐方式
复制代码 代码如下:

///
        /// 获取单元格样式
        ///

        /// Excel操作类
        /// 单元格字体
        /// 图案的颜色
        /// 图案样式
        /// 单元格背景
        /// 垂直对齐方式
        /// 垂直对齐方式
        ///
        public static ICellStyle GetCellStyle(HSSFWorkbook hssfworkbook, IFont font, HSSFColor fillForegroundColor, FillPatternType fillPattern, HSSFColor fillBackgroundColor, HorizontalAlignment ha, VerticalAlignment va)
        {
            ICellStyle cellstyle = hssfworkbook.CreateCellStyle();
            cellstyle.FillPattern = fillPattern;
            cellstyle.Alignment = ha;
            cellstyle.VerticalAlignment = va;
            if (fillForegroundColor != null)
            {
                cellstyle.FillForegroundColor = fillForegroundColor.GetIndex();
            }
            if (fillBackgroundColor != null)
            {
                cellstyle.FillBackgroundColor = fillBackgroundColor.GetIndex();
            }
            if (font != null)
            {
                cellstyle.SetFont(font);
            }
            //有边框
            cellstyle.BorderBottom = CellBorderType.THIN;
            cellstyle.BorderLeft = CellBorderType.THIN;
            cellstyle.BorderRight = CellBorderType.THIN;
            cellstyle.BorderTop = CellBorderType.THIN;
            return cellstyle;
        }

  (4).合并单元格 
复制代码 代码如下:

///
        /// 合并单元格
        ///

        /// 要合并单元格所在的sheet
        /// 开始行的索引
        /// 结束行的索引
        /// 开始列的索引
        /// 结束列的索引
        public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend)
        {
            CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);
            sheet.AddMergedRegion(cellRangeAddress);
        }

  4.将Excel文件输出

    FileStream stream = File.OpenWrite(@"F:/test.xls"); ;
            book.Write(stream);
            stream.Close();

 

  以上就是使用NPOI动态生成Excel的行和列,以及单元格的样式,具体的可以参考Demo下载.

以上就是用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程