zoukankan      html  css  js  c++  java
  • NPOI的一些基本操作

    1,创建一个Excel

     //创建一个工作簿        XSSFWorkbook workbook = new XSSFWorkbook();       
     //创建一个页        ISheet sheet = workbook.CreateSheet("sheet1");       
     //创建一行        IRow row = sheet.CreateRow(0);        
     //创建一列        ICell cell = row.CreateCell(0);
    

    2,设置字体

       ICellStyle style = workbook.CreateCellStyle();//创建样式对象        
       IFont font = workbook.CreateFont(); //创建一个字体样式对象        
       font.FontName = "方正舒体"; //和excel里面的字体对应     
       font.Color = new HSSFColor.PINK().GetIndex();//颜色参考NPOI的颜色对照表(替换掉PINK())       
       font.IsItalic = true; //斜体       
       font.FontHeightInPoints = 16;//字体大小      
       font.Boldweight = short.MaxValue;//字体加粗      
       style.SetFont(font); //将字体样式赋给样式对象       
       cell.CellStyle = style; //把样式赋给单元格
    

    3,单元格宽高

    行高:row.Height = 30 * 20;    //行高为30
    列宽:sheet.SetColumnWidth(3, 13 * 256)   //第4列的列宽为13
    

    4,合并单元格

      单元格合并后,样式以左上角的单元格为准
      //CellRangeAddress四个参数为:起始行,结束行,起始列,结束列
      sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));
    

    5,水平居中

    style.Alignment = HorizontalAlignment.CENTER;
    

    6,设置公式

      不需要写“=”号
      cell.CellFormula = "公式";
    

    7,边框

    //上下左右
    styleFont.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;     
    styleFont.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;   
    styleFont.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;   
    styleFont.BorderRight = NPOI.SS.UserModel.BorderStyle.THICK;
    
  • 相关阅读:
    对象池使用时要注意几点
    Flash3D学习计划(一)——3D渲染的一般管线流程
    714. Best Time to Buy and Sell Stock with Transaction Fee
    712. Minimum ASCII Delete Sum for Two Strings
    647. Palindromic Substrings(马拉车算法)
    413. Arithmetic Slices
    877. Stone Game
    338. Counting Bits
    303. Range Sum Query
    198. House Robber
  • 原文地址:https://www.cnblogs.com/ylzhang/p/7171241.html
Copyright © 2011-2022 走看看