1 应用组件

using NPOI.SS.UserModel;

using NPOI.HSSF.Util;

2.一个简单demo

  

2.1 定义单元格常用到样式的枚举

public enum stylexls
{

shead,//头
surl,
sdate,//时间
snumber,//数字
smoney,//钱
spercentage,//百分比
schinaUpper,//中文大写
scientific,//科学计数法
sdefault //默认
}

2.2定义单元格常用到样式

static ICellStyle Getcellstyle(IWorkbook wb, stylexls str)
{
ICellStyle cellStyle = wb.CreateCellStyle();

//定义几种字体
//也可以一种字体,写一些公共属性,然后在下面需要时加特殊的
IFont font12 = wb.CreateFont();
font12.FontHeightInPoints = 10;
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.FillForegroundColor = HSSFColor.WHITE.index;
cellStyle.FillBackgroundColor = HSSFColor.BLUE.index;

//水平对齐
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;

//垂直对齐
cellStyle.VerticalAlignment = VerticalAlignment.CENTER;

//自动换行
cellStyle.WrapText = true;

//缩进;
cellStyle.Indention = 0;

//上面基本都是设共公的设置
//下面列出了常用的字段类型
switch (str)
{
case stylexls.shead:
// cellStyle.FillPattern = FillPatternType.LEAST_DOTS;
cellStyle.SetFont(font12);
break;
case stylexls.sdate:
IDataFormat datastyle = wb.CreateDataFormat();

cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd");
cellStyle.SetFont(font);
break;
case stylexls.snumber:
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
cellStyle.SetFont(font);
break;
case stylexls.smoney:
IDataFormat format = wb.CreateDataFormat();
cellStyle.DataFormat = format.GetFormat("¥#,##0");
cellStyle.SetFont(font);
break;
case stylexls.surl:
fontcolorblue.Underline = 1;
cellStyle.SetFont(fontcolorblue);
break;
case stylexls.spercentage:
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
cellStyle.SetFont(font);
break;
case stylexls.schinaUpper:
IDataFormat format1 = wb.CreateDataFormat();
cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0");
cellStyle.SetFont(font);
break;
case stylexls.scientific:
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00");
cellStyle.SetFont(font);
break;
case stylexls.sdefault:
cellStyle.SetFont(font);
break;
}
return cellStyle;

}

2.3创建一个常用的xls文件

IWorkbook wb = new HSSFWorkbook();
//创建表
ISheet sh = wb.CreateSheet("索引一");
//设置单元的宽度
sh.SetColumnWidth(0, 15 * 256);
sh.SetColumnWidth(1, 35 * 256);
sh.SetColumnWidth(2, 15 * 256);
sh.SetColumnWidth(3, 10 * 256);
#region 练习合并单元格
sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 3));

IRow row0 = sh.CreateRow(0);
row0.Height = 20 * 20;
ICell icell1top0 = row0.CreateCell(0);
icell1top0.CellStyle = Getcellstyle(wb, stylexls.shead);
icell1top0.SetCellValue("角色人物属性列表");
#endregion

#region 设置表头
IRow row1 = sh.CreateRow(1);
row1.Height = 20 * 20;

ICell icell1top = row1.CreateCell(0);
icell1top.CellStyle = Getcellstyle(wb, stylexls.shead);
icell1top.SetCellValue("角色");

ICell icell2top = row1.CreateCell(1);
icell2top.CellStyle = Getcellstyle(wb, stylexls.shead);
icell2top.SetCellValue("攻击属性");

ICell icell3top = row1.CreateCell(2);
icell3top.CellStyle = Getcellstyle(wb, stylexls.shead);
icell3top.SetCellValue("防御属性");

ICell icell4top = row1.CreateCell(3);
icell4top.CellStyle = Getcellstyle(wb, stylexls.shead);
icell4top.SetCellValue("附加属性");
#endregion

#region 数据绑定
for (int i = 3; i < 5; i++){

IRow row = sh.CreateRow(i);
for (int j = 0; j < 4; j++)
{
string values =j==0?"JS"+j:j==1?"100+":"150";
ICell cell = row.CreateCell(j);
cell.CellStyle = Getcellstyle(wb, stylexls.sdefault);
cell.SetCellValue(values);
}
}
#endregion
using (FileStream stm = File.OpenWrite(@"c:/myMergeCell.xls"))
{
wb.Write(stm);
}

NPOI 1.0的更多相关文章

  1. c#.Net:Excel导入/导出之NPOI 2.0简介

      NPOI 2.0+主要由SS, HPSF, DDF, HSSF, XWPF, XSSF, OpenXml4Net, OpenXmlFormats组成,具体列表如下: 资料来自:百度百科   Ass ...

  2. NPOI 2.0 Excel读取显示

    NPOI 2.0 Excel读取显示   最近接到需求,需要把excel表格里的数据原样展示到web页面,主要是满足随意跨行跨列. 之前用过一点NPOI,不过接触的不太多,趁这次机会再熟悉一下.由于操 ...

  3. NPOI 2.0 教程(二):编辑既存的EXCEL文件

    NPOI 2.0 教程(二):编辑既存的EXCEL文件 分类: C#技术 2014-03-11 15:40 993人阅读 评论(3) 收藏 举报 c#excelNPOI 转载请注明出处 http:// ...

  4. NPOI 2.0 教程

    NPOI2.0帮助官方地址 目录 1. 前言 1.1 NPOI 2.0与NPOI 1.x的区别 1.2 NPOI 2.0模块简介 1.3 自动识别并打开Excel 2003和Excel 2007文件 ...

  5. NPOI 2.0版本的使用

    详细教程: http://blog.csdn.net/xxs77ch/article/details/50216033 using System; using System.Collections.G ...

  6. NPOI 2.0 读取、编辑、保存Excel文件

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

  7. NPOI 2.0 创建Excel文件

    如果只是简单的处理的话,只需要引用下载压缩包里的 NPOI.dll (office 2003)或 NPOI.OOXML.dll (office 2007) 文件而已. using System; us ...

  8. NPOI 2.0导出word(docx格式)

    大名鼎鼎的NPOI用来导出EXCEL的文章园子里面有很多,可是用来导出WORD文档的文章大都含糊不清,最近刚好完成一个导出WORD文档的需求,在此分享下. NPOI里面认为word文档的最基本的结构是 ...

  9. NPOI操作EXCEL(一)——npoi基础

    去年项目有一个子模块需要解析上百张不一样的excel表格入库,当时用的NPOI,做了很久...也尝试想把代码分享到oschina,结果没坚持两篇就放弃了. 赶巧的是,昨天运营那边提出要录入一些基础数据 ...

随机推荐

  1. Java-MyBatis-MyBatis3-XML映射文件:XML映射文件

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:XML映射文件 1. XML 映射文件 MyBatis 的真正强大在于它的映射语句,这是它的魔力所在.由于它的异常强大,映射 ...

  2. iOS界面动画特效

    1.TableView的headView背景图片拉伸 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup aft ...

  3. 【JEECG-Boot 技术文档】新手入门教程

    Jeecg-Boot入门教程必看(新手学习) 1.开发环境搭建 http://jeecg-boot.mydoc.io/?t=345670 开发工具 :https://pan.baidu.com/sha ...

  4. 转:五种I/O模型和select函数简介

    源地址:http://blog.csdn.net/jnu_simba/article/details/9070955 一.五种I/O模型 1.阻塞I/O 我们在前面所说的I/O模型都是阻塞I/O,即调 ...

  5. My solutions to the exercises in "The Boost C++ Libraries"

    I like books with excercises, but I also want solutions to see if I got it right. When working throu ...

  6. 删除文件夹时提示“You need permission to perform this action。。。”,如何解决?

    Win10系统,有时,要删除某个文件夹,却提示“You need permission to perform this action...” 以下是我Google之后找到的解决方案 1.创建一个文本文 ...

  7. 解决IDEA maven多模块打包问题

    参考: https://www.jianshu.com/p/37c6688c4fcb https://blog.csdn.net/sjhuangx/article/details/71519066 h ...

  8. 04.Hibernate常用的接口和类---SessionFactory类和作用

    是一个生成Session的工厂类 特点: 1.由Configuration通过加载配置文件创建该对象. SessionFactory factory = config.buildSessionFact ...

  9. [JZOJ5229]【GDOI2018模拟7.14】小奇的糖果

    题目 题目大意 在一个二维的平面上,有一堆有颜色的点,你需要找出一条水平线段,使得这个线段上面(或者是下面)的点的颜色不包含所有的颜色.问点数最大是多少. 思考历程 在一开始,我看错了题目大意. 题目 ...

  10. 玩转python爬虫之正则表达式

    玩转python爬虫之正则表达式 这篇文章主要介绍了python爬虫的正则表达式,正则表达式在Python爬虫是必不可少的神兵利器,本文整理了Python中的正则表达式的相关内容,感兴趣的小伙伴们可以 ...