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

简介: 本篇文章小编为大家介绍,用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法。需要的朋友参考下 今天在做项目中,遇到使用代码生成具有一定样式的Excel,找了很多资料,最后终于解决了,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).创建单元格字体的样式及大小

 代码如下:

/// <summary>
        /// 获取字体样式
        /// </summary>
        /// <param name="hssfworkbook">Excel操作类</param>
        /// <param name="fontname">字体名</param>
        /// <param name="fontcolor">字体颜色</param>
        /// <param name="fontsize">字体大小</param>
        /// <returns></returns>
        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).创建单元格的边框,背景颜色,以及对齐方式

 代码如下:

/// <summary>
        /// 获取单元格样式
        /// </summary>
        /// <param name="hssfworkbook">Excel操作类</param>
        /// <param name="font">单元格字体</param>
        /// <param name="fillForegroundColor">图案的颜色</param>
        /// <param name="fillPattern">图案样式</param>
        /// <param name="fillBackgroundColor">单元格背景</param>
        /// <param name="ha">垂直对齐方式</param>
        /// <param name="va">垂直对齐方式</param>
        /// <returns></returns>
        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).合并单元格 

 代码如下:

/// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="sheet">要合并单元格所在的sheet</param>
        /// <param name="rowstart">开始行的索引</param>
        /// <param name="rowend">结束行的索引</param>
        /// <param name="colstart">开始列的索引</param>
        /// <param name="colend">结束列的索引</param>
        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的行和列,以及单元格的样式

相关文章
|
1月前
|
存储 数据处理 索引
Python操作Excel常用方法汇总
Python操作Excel常用方法汇总
33 0
|
1月前
|
存储 数据可视化 数据处理
Python中读取Excel文件的方法
【2月更文挑战第18天】
66 4
Python中读取Excel文件的方法
excel中同一单元格内容分隔到不同的单元格
excel中同一单元格内容分隔到不同的单元格
excel中同一单元格内容分隔到不同的单元格
|
3月前
|
BI 索引 Python
python报表自动化系列 - 译码:与Excel单元格索引对应的十进制数坐标
python报表自动化系列 - 译码:与Excel单元格索引对应的十进制数坐标
17 1
|
3月前
|
BI 索引 Python
python报表自动化系列 - Excel单元格(Cell)索引范围对应的所有单元格
python报表自动化系列 - Excel单元格(Cell)索引范围对应的所有单元格
26 0
|
30天前
|
存储 SQL C#
C#实现Excel合并单元格数据导入数据集
C#实现Excel合并单元格数据导入数据集
|
1月前
|
数据处理 Python
4种方法用Python批量实现多Excel多Sheet合并
4种方法用Python批量实现多Excel多Sheet合并
34 0
|
2月前
|
算法 前端开发
Excel 表中某个范围内的单元格
Excel 表中某个范围内的单元格
19 0
excel双纵坐标轴展示的方法
excel双纵坐标轴展示的方法
excel中组合不同单元格的文字并实现单元格内换行
excel中组合不同单元格的文字并实现单元格内换行