国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

用NPOI創(chuàng)建Excel、合并單元格、設置單元格樣式、邊框的方法

2020-01-24 03:25:49
字體:
來源:轉載
供稿:網(wǎng)友

今天在做項目中,遇到使用代碼生成具有一定樣式的Excel,找了很多資料,最后終于解決了,Excel中格式的設置,以及單元格的合并等等。下面就介紹下,使用NPOI類庫操作Excel的方法。

  1.首先我們先在內存中生成一個Excel文件,代碼如下:

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

  2.然后在新創(chuàng)建的sheet里面,創(chuàng)建我們的行和列,代碼如下:

復制代碼 代碼如下:

IRow row = sheet.CreateRow(index);//index代表多少行
row.HeightInPoints = 35;//行高
ICell cell = row.CreateCell(0);//創(chuàng)建第一列
cell.SetCellValue(“設置單元格的值”);

  3.設置單元格的樣式已經(jīng)字體大小,邊框,以及合并單元格

  (1).創(chuàng)建單元格字體的樣式及大小

復制代碼 代碼如下:

/// <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).設置單元格內顯示數(shù)據(jù)的格式
復制代碼 代碼如下:

ICell cell = row.CreateCell(1);
ICellStyle cellStyleNum = Excel.GetICellStyle(book);
IDataFormat formatNum = book.CreateDataFormat();
cellStyleNum.DataFormat = formatNum.GetFormat("0.00E+00");//設置單元格的格式為科學計數(shù)法cell.CellStyle = cellStyleNum;

  (3).創(chuàng)建單元格的邊框,背景顏色,以及對齊方式
復制代碼 代碼如下:

/// <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動態(tài)生成Excel的行和列,以及單元格的樣式,具體的可以參考Demo下載.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 嘉鱼县| 咸丰县| 饶河县| 潮安县| 海兴县| 大港区| 定襄县| 宿迁市| 乌苏市| 页游| 察哈| 凭祥市| 池州市| 阿拉善盟| 闻喜县| 延川县| 桂阳县| 阿图什市| 轮台县| 内江市| 民丰县| 利津县| 潮州市| 宁都县| 大悟县| 茌平县| 望江县| 赣榆县| 嘉荫县| 大厂| 陆丰市| 资兴市| 阿拉善盟| 布拖县| 巍山| 奎屯市| 墨脱县| 麻栗坡县| 荆门市| 徐州市| 巍山|