using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Eval;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.POIFS.FileSystem;
using NPOI.HPSF;
using System.IO;
using NPOI.SS.Util;
using System.Drawing;
using NPOI.HSSF.Util; namespace NPOI
{
class Program7
{
static void Main(string[] args)
{
//说明:设置数字格式 //1.创建EXCEL中的Workbook
IWorkbook myworkbook = new XSSFWorkbook(); //2.创建Workbook中的Sheet
ISheet mysheet = myworkbook.CreateSheet("sheet1");
mysheet.SetColumnWidth(0, 20 * 256);
mysheet.SetColumnWidth(1, 20 * 256); //3.创建Row中的Cell并赋值
IRow row0 = mysheet.CreateRow(0); row0.CreateCell(0).SetCellValue(2013.143); row0.CreateCell(1).SetCellValue("转化为汉字大写");
IRow row1 = mysheet.CreateRow(1); row1.CreateCell(0).SetCellValue(123152013.143); row1.CreateCell(1).SetCellValue("改变小数精度");
IRow row2 = mysheet.CreateRow(2); row2.CreateCell(0).SetCellValue(123152013.143); row2.CreateCell(1).SetCellValue("分段添加,号");
IRow row3 = mysheet.CreateRow(3); row3.CreateCell(0).SetCellValue(123152013.143); row3.CreateCell(1).SetCellValue("科学计数法");
IRow row4 = mysheet.CreateRow(4); row4.CreateCell(0).SetCellValue(-123152013.143); row4.CreateCell(1).SetCellValue("正数与负数的区分(负数红色)");
IRow row5 = mysheet.CreateRow(5); row5.CreateCell(0).SetCellValue(123152013.77); row5.CreateCell(1).SetCellValue("整数部分+分数");
IRow row6 = mysheet.CreateRow(6); row6.CreateCell(0).SetCellValue(123152013.77); row6.CreateCell(1).SetCellValue("分数");
IRow row7 = mysheet.CreateRow(7); row7.CreateCell(0).SetCellValue(0.333); row7.CreateCell(1).SetCellValue("百分数"); //4.创建CellStyle与DataFormat并加载格式样式
IDataFormat dataformat = myworkbook.CreateDataFormat(); ICellStyle style0 = myworkbook.CreateCellStyle();
style0.DataFormat = dataformat.GetFormat("[DbNum2][$-804]General");//转化为汉字大写 ICellStyle style1 = myworkbook.CreateCellStyle();
style1.DataFormat = dataformat.GetFormat("0.0"); //改变小数精度【小数点后有几个0表示精确到小数点后几位】 ICellStyle style2 = myworkbook.CreateCellStyle();
style2.DataFormat = dataformat.GetFormat("#,##0.0");//分段添加,号 ICellStyle style3 = myworkbook.CreateCellStyle();
style3.DataFormat = dataformat.GetFormat("0.00E+00");//科学计数法 ICellStyle style4 = myworkbook.CreateCellStyle();
style4.DataFormat = dataformat.GetFormat("0.00;[Red]-0.00");//正数与负数的区分 ICellStyle style5 = myworkbook.CreateCellStyle();
style5.DataFormat = dataformat.GetFormat("# ??/??");//整数部分+分数 ICellStyle style6 = myworkbook.CreateCellStyle();
style6.DataFormat = dataformat.GetFormat("??/??");//分数 ICellStyle style7 = myworkbook.CreateCellStyle();
style7.DataFormat = dataformat.GetFormat("0.00%");//百分数【小数点后有几个0表示精确到显示小数点后几位】 //5.将CellStyle应用于具体单元格
row0.GetCell(0).CellStyle = style0;
row1.GetCell(0).CellStyle = style1;
row2.GetCell(0).CellStyle = style2;
row3.GetCell(0).CellStyle = style3;
row4.GetCell(0).CellStyle = style4;
row5.GetCell(0).CellStyle = style5;
row6.GetCell(0).CellStyle = style6;
row7.GetCell(0).CellStyle = style7; //6.保存
FileStream file = new FileStream(@"E:\myworkbook7.xlsx", FileMode.Create);
myworkbook.Write(file);
file.Close();
}
}
}

  

运行后,效果如下图所示【演示了不同数字格式的设置】

转载:http://blog.csdn.net/xxs77ch/article/details/50237017

npoi 设置单元格格式的更多相关文章

  1. NPOI设置单元格格式

    转自:http://www.cr173.com/html/18143_2.html //创建一个常用的xls文件 private void button3_Click(object sender, E ...

  2. C#:org.in2bits.MyXls 文本格式日期 转换,以及设置单元格格式,保留两位小数点

    org.in2bits.MyXls  Excel导入日期格式的处理 表格内容为 2014-7-22 ,导入后显示为 41842 等于一个数值,根本不是日期,后来百度了一下,发现要做如下处理: stri ...

  3. NPOI设置单元格背景色

    NPOI设置单元格背景色在网上有好多例子都是设置为NPOI内置的颜色值 但是想用rgb值来设置背景色,即:通过HSSFPalette类获取颜色值时会抛出异常:Could not Find free c ...

  4. Aspose.Cells设置单元格格式

    使用Aspose.Cells操作Excel时,填写的参数是这样的,显然要不得! 这需要像Excel中的“转换为数字”操作,强大的Aspose.Cells可轻松解决这个问题. //默认写法 worksh ...

  5. 导出EXCEL设置单元格格式

    怎么设置导出的EXCEL文件的列格式 如何设置导出的EXCEL文件的列格式在office的EXCEL中我们可以在一个EXCEL文件中,选中一列再点击鼠标右键,选择设置单元格格式,可以将这一列设为文本格 ...

  6. python从数据库取数据后写入excel 使用pandas.ExcelWriter设置单元格格式

    用python从数据库中取到数据后,写入excel中做成自动报表,ExcelWrite默认的格式一般来说都比较丑,但workbook提供可以设置自定义格式,简单记录个demo,供初次使用者参考. 一. ...

  7. phpexcel来做表格导出(多个工作sheet)及设置单元格格式

    <?php /** * 简单实用Execl */ set_include_path('.'.get_include_path().PATH_SEPARATOR.dirname(__FILE__) ...

  8. C#导出Excel,并且设置Excel单元格格式,合并单元格.

    注:要添加COM组件 Microsoft Excel 11.0 Object Library  引用. 具体代码如下: using System; using System.Collections.G ...

  9. NPOI自定义单元格背景颜色

    经常在NPOI群里聊天时发现有人在问NPOI设置单元格背景颜色的问题,而Tony Qu大神的博客里没有相关教程,刚好最近在做项目时研究了一下这一块,在这里总结一下. 在NPOI中默认的颜色类是HSSF ...

随机推荐

  1. 抓包工具Burp Suite安装步骤(待补充)

    Burp Suite V2.1(破解版)安装步骤:(可以自行下载破解版或汉化版) 百度网盘下载地址: 链接:https://pan.baidu.com/s/1bU5JME3OsEsXrSirTdesR ...

  2. ajax之---“伪”ajax

    views.py def ajax1(request): ret={'status':'true','message':'successful'} return HttpResponse(json.d ...

  3. [LeetCode]739. 每日温度(单调栈)

    题目 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高超过该日的天数.如果之后都不会升高,请在该位置用 0 来代替. 例如,给定一个列表 temperatures ...

  4. Tomcat 第二篇:启动流程

    1 Tomcat 基本知识 首先介绍一些 Tomcat 的基本知识,防止有纯小白看的云里雾里的. 下面这张图是一个下载好二进制版的的 Tomcat ,直接解压得到的,虽然版本是 9.x ,但是这个目录 ...

  5. C++11 随机数生成器

    背景 考试想造浮点数然后发现不会 正好下午被虎哥茶话会 谈到了一些不会的问题balabala的 被告知\(C++11\)有些神奇特性(哦豁) 然后就学习了一手看上去没什么用的随机数生成器\(QwQ\) ...

  6. web自动化(python)——selenium工具基本使用

    WebDriver基本操作 生成driver--启动浏览器 #启动谷歌浏览器,预先安装chromedrvier插件 from selenium import webdriver driver = we ...

  7. Docker 容器化部署 Python 应用

    Docker 是一个开源项目,为开发人员和系统管理员提供了一个开放平台,可以将应用程序构建.打包为一个轻量级容器,并在任何地方运行.Docker 会在软件容器中自动部署应用程序. 在本篇中,我将介绍如 ...

  8. Git的使用--码云

    Git的使用--码云 进入码云官网:https://gitee.com/ 注册or登录账号进入gitee页面(页面结构大同小异). 点击右上角加号--新建仓库,用于存放项目代码 创建项目需要注意的选项 ...

  9. spark 四种模式

    Spark 三种运行模式  一:Spark On Local     此种模式下,我们只需要在安装Spark时不进行hadoop和Yarn的环境配置,只要将Spark包解压即可使用,运行时Spark目 ...

  10. vue安装教程

    Vue.js 安装教程 安装node.js https://nodejs.org/zh-cn/download/ 选择一个适合自己电脑的版本下载 下载成功, 直接安装, 全部点击下一步 然后输入 黑窗 ...