转自:http://www.cr173.com/html/18143_2.html

//创建一个常用的xls文件
private void button3_Click(object sender, EventArgs e)
{
IWorkbook wb = new HSSFWorkbook();
//创建表
ISheet sh = wb.CreateSheet("zhiyuan");
//设置单元的宽度
sh.SetColumnWidth(, * );
sh.SetColumnWidth(, * );
sh.SetColumnWidth(, * );
sh.SetColumnWidth(, * );
int i = ;
#region 练习合并单元格
sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(, , , )); //CellRangeAddress()该方法的参数次序是:开始行号,结束行号,开始列号,结束列号。 IRow row0 = sh.CreateRow();
row0.Height = * ;
ICell icell1top0 = row0.CreateCell();
icell1top0.CellStyle = Getcellstyle(wb, stylexls.头);
icell1top0.SetCellValue("标题合并单元");
#endregion
i++;
#region 设置表头
IRow row1 = sh.CreateRow();
row1.Height = * ; ICell icell1top = row1.CreateCell();
icell1top.CellStyle = Getcellstyle(wb, stylexls.头);
icell1top.SetCellValue("网站名"); ICell icell2top = row1.CreateCell();
icell2top.CellStyle = Getcellstyle(wb, stylexls.头);
icell2top.SetCellValue("网址"); ICell icell3top = row1.CreateCell();
icell3top.CellStyle = Getcellstyle(wb, stylexls.头);
icell3top.SetCellValue("百度快照"); ICell icell4top = row1.CreateCell();
icell4top.CellStyle = Getcellstyle(wb, stylexls.头);
icell4top.SetCellValue("百度收录");
#endregion using(FileStream stm=File.OpenWrite(@"c:/myMergeCell.xls"))
{
wb.Write(stm);
MessageBox.Show("提示:创建成功!");
}
} #region 定义单元格常用到样式的枚举
public enum stylexls
{
头,
url,
时间,
数字,
钱,
百分比,
中文大写,
科学计数法,
默认
}
#endregion #region 定义单元格常用到样式
static ICellStyle Getcellstyle(IWorkbook wb, stylexls str)
{
ICellStyle cellStyle = wb.CreateCellStyle(); //定义几种字体
//也可以一种字体,写一些公共属性,然后在下面需要时加特殊的
IFont font12 = wb.CreateFont();
font12.FontHeightInPoints = ;
font12.FontName = "微软雅黑"; IFont font = wb.CreateFont();
font.FontName = "微软雅黑";
//font.Underline = 1;下划线 IFont fontcolorblue = wb.CreateFont();
fontcolorblue.Color = HSSFColor.OLIVE_GREEN.BLUE.index;
fontcolorblue.IsItalic = true;//下划线
fontcolorblue.FontName = "微软雅黑"; //边框
cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;
cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.HAIR;
cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.HAIR;
cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.DOTTED;
//边框颜色
cellStyle.BottomBorderColor = HSSFColor.OLIVE_GREEN.BLUE.index;
cellStyle.TopBorderColor = HSSFColor.OLIVE_GREEN.BLUE.index; //背景图形,我没有用到过。感觉很丑
//cellStyle.FillBackgroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;
//cellStyle.FillForegroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;
cellStyle.FillForegroundColor = HSSFColor.WHITE.index;
// cellStyle.FillPattern = FillPatternType.NO_FILL;
cellStyle.FillBackgroundColor = HSSFColor.BLUE.index; //水平对齐
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT; //垂直对齐
cellStyle.VerticalAlignment = VerticalAlignment.CENTER; //自动换行
cellStyle.WrapText = true; //缩进;当设置为1时,前面留的空白太大了。希旺官网改进。或者是我设置的不对
cellStyle.Indention = ; //上面基本都是设共公的设置
//下面列出了常用的字段类型
switch (str)
{
case stylexls.头:
// cellStyle.FillPattern = FillPatternType.LEAST_DOTS;
cellStyle.SetFont(font12);
break;
case stylexls.时间:
IDataFormat datastyle = wb.CreateDataFormat(); cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd");
cellStyle.SetFont(font);
break;
case stylexls.数字:
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
cellStyle.SetFont(font);
break;
case stylexls.钱:
IDataFormat format = wb.CreateDataFormat();
cellStyle.DataFormat = format.GetFormat("¥#,##0");
cellStyle.SetFont(font);
break;
case stylexls.url:
fontcolorblue.Underline = ;
cellStyle.SetFont(fontcolorblue);
break;
case stylexls.百分比:
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
cellStyle.SetFont(font);
break;
case stylexls.中文大写:
IDataFormat format1 = wb.CreateDataFormat();
cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0");
cellStyle.SetFont(font);
break;
case stylexls.科学计数法:
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00");
cellStyle.SetFont(font);
break;
case stylexls.默认:
cellStyle.SetFont(font);
break;
}
return cellStyle; }
#endregion

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

  1. npoi 设置单元格格式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  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. 二叉苹果树|codevs5565|luoguP2015|树形DP|Elena

    二叉苹果树 题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的 ...

  2. 对iphone手机IMU的陀螺仪、加速度计、图像的时间戳做对齐处理

    https://blog.csdn.net/chishuideyu/article/details/77479758 加速度计和陀螺仪的时间戳一致 ros 的message filters可以做时间同 ...

  3. 手把手教你用Spring Cloud和Docker构建微服务

    什么是Spring Cloud? Spring Cloud 是Pivotal提供的用于简化分布式系统构建的工具集.Spring Cloud引入了云平台连接器(Cloud Connector)和服务连接 ...

  4. 超级有用的15个mysqlbinlog命令

    在MySQL或MariaDB中,任意时间对数据库所做的修改,都会被记录到日志文件中.例如,当你添加了一个新的表,或者更新了一条数据,这些事件都会被存储到二进制日志文件中.二进制日志文件在MySQL主从 ...

  5. XA Transactions Restrictions on XA Transactions

    小结: 1.innodb支持XA事务: 2.XA协议作为资源管理器(数据库)与事务管理器的接口标准: 3.提交或者回滚的点:必须所有的组件被提交或者被回滚: 4.2阶段 PC-1,TM告知所有RM要准 ...

  6. wpf数据绑定:xml数据绑定

    wpf中可以通过XmlDataProvider,来实现xml数据的绑定.它通过XmlDataProvider来绑定外部资源,需要命名一个 x:Key 值,以便数据绑定目标可对其进行引用,Source设 ...

  7. iOS-静态库,动态库,framework浅析(二)

    创建.a静态库 第一步,新建工程.     一般使用工程名就使用库的名称,比如我这里用FMDB来创建静态库,我的工程名就取名为FMDB,创建的.a静态库就是libFMDB.a.             ...

  8. 转:centos7/rhel7安装较高版本ruby2.2/2.3/2.4+

    centos7/rhel7安装较高版本ruby2.2/2.3/2.4+   环境需求:  在Centos7.3中,通过yum安装ruby的版本是2.0.0,但是如果有些应用需要高版本的ruby环境,比 ...

  9. SpringMVC 的使用映射路径 <mvc:resources >

    以下是测试结果,可能存在纰漏,暂记录一下. 使用springMVC时,一般将DispatcherServlet请求映射配置为"/",则Spring MVC将捕获Web容器所有的请求 ...

  10. JavaScript, DOM查找元素

    1.document.getElementById("id"); => IE8 及较低版本不区分ID的大小写 => IE7及较低版本中表单元素的name特性和ID都会被 ...