zoukankan      html  css  js  c++  java
  • 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的行和列,以及单元格的样式,具体的可以参考Demo下载.

      PS:详细的可以参考 http://tonyqus.sinaapp.com/tutorial

        单元格的格式设置,可以参考http://tech.ddvip.com/2009-03/1238143235112434.html

        http://wenku.baidu.com/view/035c53d0c1c708a1284a4444.html

  • 相关阅读:
    Sql2000分页效率
    CSS笔记
    向模态窗体传递参数和获取返回值
    css 实现div 内容垂直居中
    轻量级的数据交换格式——初识Json
    CSS 绝对定位
    前台小模块CSS布局代码
    XML常用类(淘宝API)
    表单form
    js 分页
  • 原文地址:https://www.cnblogs.com/akwwl/p/3041282.html
Copyright © 2011-2022 走看看