NPOI2.2.0.0实例详解(八)—设置EXCEL单元格【数字格式】

时间:2023-03-08 18:01:26
  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. namespace NPOI
  18. {
  19. class Program7
  20. {
  21. static void Main(string[] args)
  22. {
  23. //说明:设置数字格式
  24. //1.创建EXCEL中的Workbook
  25. IWorkbook myworkbook = new XSSFWorkbook();
  26. //2.创建Workbook中的Sheet
  27. ISheet mysheet = myworkbook.CreateSheet("sheet1");
  28. mysheet.SetColumnWidth(0, 20 * 256);
  29. mysheet.SetColumnWidth(1, 20 * 256);
  30. //3.创建Row中的Cell并赋值
  31. IRow row0 = mysheet.CreateRow(0); row0.CreateCell(0).SetCellValue(2013.143); row0.CreateCell(1).SetCellValue("转化为汉字大写");
  32. IRow row1 = mysheet.CreateRow(1); row1.CreateCell(0).SetCellValue(123152013.143); row1.CreateCell(1).SetCellValue("改变小数精度");
  33. IRow row2 = mysheet.CreateRow(2); row2.CreateCell(0).SetCellValue(123152013.143); row2.CreateCell(1).SetCellValue("分段添加,号");
  34. IRow row3 = mysheet.CreateRow(3); row3.CreateCell(0).SetCellValue(123152013.143); row3.CreateCell(1).SetCellValue("科学计数法");
  35. IRow row4 = mysheet.CreateRow(4); row4.CreateCell(0).SetCellValue(-123152013.143); row4.CreateCell(1).SetCellValue("正数与负数的区分(负数红色)");
  36. IRow row5 = mysheet.CreateRow(5); row5.CreateCell(0).SetCellValue(123152013.77); row5.CreateCell(1).SetCellValue("整数部分+分数");
  37. IRow row6 = mysheet.CreateRow(6); row6.CreateCell(0).SetCellValue(123152013.77); row6.CreateCell(1).SetCellValue("分数");
  38. IRow row7 = mysheet.CreateRow(7); row7.CreateCell(0).SetCellValue(0.333); row7.CreateCell(1).SetCellValue("百分数");
  39. //4.创建CellStyle与DataFormat并加载格式样式
  40. IDataFormat dataformat = myworkbook.CreateDataFormat();
  41. ICellStyle style0 = myworkbook.CreateCellStyle();
  42. style0.DataFormat = dataformat.GetFormat("[DbNum2][$-804]General");//转化为汉字大写
  43. ICellStyle style1 = myworkbook.CreateCellStyle();
  44. style1.DataFormat = dataformat.GetFormat("0.0"); //改变小数精度【小数点后有几个0表示精确到小数点后几位】
  45. ICellStyle style2 = myworkbook.CreateCellStyle();
  46. style2.DataFormat = dataformat.GetFormat("#,##0.0");//分段添加,号
  47. ICellStyle style3 = myworkbook.CreateCellStyle();
  48. style3.DataFormat = dataformat.GetFormat("0.00E+00");//科学计数法
  49. ICellStyle style4 = myworkbook.CreateCellStyle();
  50. style4.DataFormat = dataformat.GetFormat("0.00;[Red]-0.00");//正数与负数的区分
  51. ICellStyle style5 = myworkbook.CreateCellStyle();
  52. style5.DataFormat = dataformat.GetFormat("# ??/??");//整数部分+分数
  53. ICellStyle style6 = myworkbook.CreateCellStyle();
  54. style6.DataFormat = dataformat.GetFormat("??/??");//分数
  55. ICellStyle style7 = myworkbook.CreateCellStyle();
  56. style7.DataFormat = dataformat.GetFormat("0.00%");//百分数【小数点后有几个0表示精确到显示小数点后几位】
  57. //5.将CellStyle应用于具体单元格
  58. row0.GetCell(0).CellStyle = style0;
  59. row1.GetCell(0).CellStyle = style1;
  60. row2.GetCell(0).CellStyle = style2;
  61. row3.GetCell(0).CellStyle = style3;
  62. row4.GetCell(0).CellStyle = style4;
  63. row5.GetCell(0).CellStyle = style5;
  64. row6.GetCell(0).CellStyle = style6;
  65. row7.GetCell(0).CellStyle = style7;
  66. //6.保存
  67. FileStream file = new FileStream(@"E:\myworkbook7.xlsx", FileMode.Create);
  68. myworkbook.Write(file);
  69. file.Close();
  70. }
  71. }
  72. }