npoi 设置单元格格式
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 设置单元格格式的更多相关文章
- NPOI设置单元格格式
转自:http://www.cr173.com/html/18143_2.html //创建一个常用的xls文件 private void button3_Click(object sender, E ...
- C#:org.in2bits.MyXls 文本格式日期 转换,以及设置单元格格式,保留两位小数点
org.in2bits.MyXls Excel导入日期格式的处理 表格内容为 2014-7-22 ,导入后显示为 41842 等于一个数值,根本不是日期,后来百度了一下,发现要做如下处理: stri ...
- NPOI设置单元格背景色
NPOI设置单元格背景色在网上有好多例子都是设置为NPOI内置的颜色值 但是想用rgb值来设置背景色,即:通过HSSFPalette类获取颜色值时会抛出异常:Could not Find free c ...
- Aspose.Cells设置单元格格式
使用Aspose.Cells操作Excel时,填写的参数是这样的,显然要不得! 这需要像Excel中的“转换为数字”操作,强大的Aspose.Cells可轻松解决这个问题. //默认写法 worksh ...
- 导出EXCEL设置单元格格式
怎么设置导出的EXCEL文件的列格式 如何设置导出的EXCEL文件的列格式在office的EXCEL中我们可以在一个EXCEL文件中,选中一列再点击鼠标右键,选择设置单元格格式,可以将这一列设为文本格 ...
- python从数据库取数据后写入excel 使用pandas.ExcelWriter设置单元格格式
用python从数据库中取到数据后,写入excel中做成自动报表,ExcelWrite默认的格式一般来说都比较丑,但workbook提供可以设置自定义格式,简单记录个demo,供初次使用者参考. 一. ...
- phpexcel来做表格导出(多个工作sheet)及设置单元格格式
<?php /** * 简单实用Execl */ set_include_path('.'.get_include_path().PATH_SEPARATOR.dirname(__FILE__) ...
- C#导出Excel,并且设置Excel单元格格式,合并单元格.
注:要添加COM组件 Microsoft Excel 11.0 Object Library 引用. 具体代码如下: using System; using System.Collections.G ...
- NPOI自定义单元格背景颜色
经常在NPOI群里聊天时发现有人在问NPOI设置单元格背景颜色的问题,而Tony Qu大神的博客里没有相关教程,刚好最近在做项目时研究了一下这一块,在这里总结一下. 在NPOI中默认的颜色类是HSSF ...
随机推荐
- 提升开发幸福度-IDE配置
插件 vscode插件 Settings Sync Atom One Dark Theme Bracket Pair Colorizer Code Runner Dracular Official E ...
- path.resolve和path.join的区别
// test.js const path = require('path') let x1 = path.resolve('/目录1/目录2', '/目录3/目录4/') let x2 = path ...
- selenuim常用api
1.通过代码跳转到页面的方法:前提:已安装驱动,并加入到path下 安装ie驱动或火狐驱动的情况: 2.当链接跳转按钮在页面最上方时,用window.scrollTo(0,0);显示在页面最上方后,再 ...
- xss原理解析
xss->跨站脚本攻击 xss是指攻击者在网页中嵌入客户端脚本.通常是指javascript编写的一个危险代码,当用户使用浏览器浏览网页时,脚本就会在用户的浏览器上执行,从而达到攻击者的目的. ...
- k8s service对象
k8s service对象 概述 service服务也是Kubernetes里核心字眼对象之一,Kubernetes里的每一个service其实就是我们经常提起的微服务架构中的一个微服务,之前讲解 ...
- 登录、认证、token处理、前台cookie存储token
免费课程相关表设计 models的设计 from django.contrib.contenttypes.fields import GenericRelation class Course(mode ...
- list、set、map的区别和联系
结构特点 List和Set是存储单列数据的集合,Map是存储键值对这样的双列数据的集合: List中存储的数据是有顺序的,并且值允许重复:Map中存储的数据是无序的,它的键是不允许重复的,但是值是允许 ...
- 基础篇:深入JMM内存模型解析volatile、synchronized的内存语义
目录 1 java内存模型,JMM(JAVA Memory Model) 2 CPU高速缓存.MESI协议 3 指令重排序和内存屏障指令 4 happen-before原则 5 synchronize ...
- React 服务端渲染方案完美的解决方案
最近在开发一个服务端渲染工具,通过一篇小文大致介绍下服务端渲染,和服务端渲染的方式方法.在此文后面有两中服务端渲染方式的构思,根据你对服务端渲染的利弊权衡,你会选择哪一种服务端渲染方式呢? 什么是服务 ...
- 【typedef】Type definitions 自定义类型