zoukankan      html  css  js  c++  java
  • NPOI2.2.0.0实例详解(八)—设置EXCEL单元格【数字格式】

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6. using NPOI.HSSF.UserModel;  
    7. using NPOI.SS.Formula.Eval;  
    8. using NPOI.SS.Formula.Functions;  
    9. using NPOI.SS.UserModel;  
    10. using NPOI.XSSF.UserModel;  
    11. using NPOI.POIFS.FileSystem;  
    12. using NPOI.HPSF;  
    13. using System.IO;  
    14. using NPOI.SS.Util;  
    15. using System.Drawing;  
    16. using NPOI.HSSF.Util;  
    17.   
    18. namespace NPOI  
    19. {  
    20.     class Program7  
    21.     {  
    22.         static void Main(string[] args)  
    23.         {  
    24.             //说明:设置数字格式  
    25.   
    26.             //1.创建EXCEL中的Workbook           
    27.             IWorkbook myworkbook = new XSSFWorkbook();  
    28.   
    29.             //2.创建Workbook中的Sheet          
    30.             ISheet mysheet = myworkbook.CreateSheet("sheet1");  
    31.             mysheet.SetColumnWidth(0, 20 * 256);  
    32.             mysheet.SetColumnWidth(1, 20 * 256);  
    33.   
    34.             //3.创建Row中的Cell并赋值  
    35.             IRow row0 = mysheet.CreateRow(0); row0.CreateCell(0).SetCellValue(2013.143); row0.CreateCell(1).SetCellValue("转化为汉字大写");          
    36.             IRow row1 = mysheet.CreateRow(1); row1.CreateCell(0).SetCellValue(123152013.143); row1.CreateCell(1).SetCellValue("改变小数精度");  
    37.             IRow row2 = mysheet.CreateRow(2); row2.CreateCell(0).SetCellValue(123152013.143); row2.CreateCell(1).SetCellValue("分段添加,号");  
    38.             IRow row3 = mysheet.CreateRow(3); row3.CreateCell(0).SetCellValue(123152013.143); row3.CreateCell(1).SetCellValue("科学计数法");  
    39.             IRow row4 = mysheet.CreateRow(4); row4.CreateCell(0).SetCellValue(-123152013.143); row4.CreateCell(1).SetCellValue("正数与负数的区分(负数红色)");  
    40.             IRow row5 = mysheet.CreateRow(5); row5.CreateCell(0).SetCellValue(123152013.77); row5.CreateCell(1).SetCellValue("整数部分+分数");  
    41.             IRow row6 = mysheet.CreateRow(6); row6.CreateCell(0).SetCellValue(123152013.77); row6.CreateCell(1).SetCellValue("分数");  
    42.             IRow row7 = mysheet.CreateRow(7); row7.CreateCell(0).SetCellValue(0.333); row7.CreateCell(1).SetCellValue("百分数");  
    43.   
    44.             //4.创建CellStyle与DataFormat并加载格式样式  
    45.             IDataFormat dataformat = myworkbook.CreateDataFormat();  
    46.   
    47.             ICellStyle style0 = myworkbook.CreateCellStyle();  
    48.             style0.DataFormat = dataformat.GetFormat("[DbNum2][$-804]General");//转化为汉字大写  
    49.   
    50.             ICellStyle style1 = myworkbook.CreateCellStyle();  
    51.             style1.DataFormat = dataformat.GetFormat("0.0"); //改变小数精度【小数点后有几个0表示精确到小数点后几位】  
    52.   
    53.             ICellStyle style2 = myworkbook.CreateCellStyle();  
    54.             style2.DataFormat = dataformat.GetFormat("#,##0.0");//分段添加,号  
    55.   
    56.             ICellStyle style3 = myworkbook.CreateCellStyle();  
    57.             style3.DataFormat = dataformat.GetFormat("0.00E+00");//科学计数法  
    58.   
    59.             ICellStyle style4 = myworkbook.CreateCellStyle();  
    60.             style4.DataFormat = dataformat.GetFormat("0.00;[Red]-0.00");//正数与负数的区分  
    61.   
    62.             ICellStyle style5 = myworkbook.CreateCellStyle();  
    63.             style5.DataFormat = dataformat.GetFormat("# ??/??");//整数部分+分数  
    64.   
    65.             ICellStyle style6 = myworkbook.CreateCellStyle();  
    66.             style6.DataFormat = dataformat.GetFormat("??/??");//分数  
    67.   
    68.             ICellStyle style7 = myworkbook.CreateCellStyle();  
    69.             style7.DataFormat = dataformat.GetFormat("0.00%");//百分数【小数点后有几个0表示精确到显示小数点后几位】  
    70.   
    71.             //5.将CellStyle应用于具体单元格  
    72.             row0.GetCell(0).CellStyle = style0;  
    73.             row1.GetCell(0).CellStyle = style1;  
    74.             row2.GetCell(0).CellStyle = style2;  
    75.             row3.GetCell(0).CellStyle = style3;  
    76.             row4.GetCell(0).CellStyle = style4;  
    77.             row5.GetCell(0).CellStyle = style5;  
    78.             row6.GetCell(0).CellStyle = style6;  
    79.             row7.GetCell(0).CellStyle = style7;  
    80.            
    81.             //6.保存         
    82.             FileStream file = new FileStream(@"E:myworkbook7.xlsx", FileMode.Create);  
    83.             myworkbook.Write(file);  
    84.             file.Close();  
    85.         }  
    86.     }  
    87. }  
  • 相关阅读:
    Networking with standalone containers
    记filebeat内存泄漏问题分析及调优
    原创-The Salt Master has rejected this minion's public key!解决方法
    原创-某次建表失败-ERROR 1101 (42000): BLOB/TEXT column can’t have a default value
    action命令-判断判断码是否正确
    docker-docker中用户uid异常导致权限不足
    非原创-docker 6种减小镜像大小的方式
    非原创-docker update
    原创-k8s 存活探针,就绪探针与启动探针
    原创-阿里elasticsearch数据迁移
  • 原文地址:https://www.cnblogs.com/lenther2002/p/5799339.html
Copyright © 2011-2022 走看看