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);
}

最新文章

  1. Java 抽象类的理解
  2. TMethod
  3. 简单的3个SQL视图搞定所有SqlServer数据库字典
  4. java 解析汉字拼音
  5. Java数据结构之线性表(2)
  6. 使用TreeView+ListBox+TxtBox 资料管理器
  7. linux命令之ps命令
  8. swift 学习资源
  9. struts2讲义----二
  10. oracle之时间转换
  11. (大数据工程师学习路径)第二步 Vim编辑器----Vim文档编辑
  12. table边框1px
  13. Redis多实例及主从搭建
  14. HTML 中有用的字符实体
  15. OO第二单元总结
  16. Windows跨域远程连接防火墙设置
  17. CentOS 6.7下 Samba服务器的搭建与配置(share共享模式)
  18. 2.23 js处理日历控件(修改readonly属性)
  19. Structs复习 Action传递参数
  20. Xcode打包踩过的那些坑

热门文章

  1. 服务器迁移部署PosEdi
  2. SpringCloud学习笔记(四):Eureka服务注册与发现、构建步骤、集群配置、Eureka与Zookeeper的比较
  3. php构造方法(函数)基础
  4. 解决Navicat 报错:1130-host is not allowed MySQL不允许从远程访问的方法
  5. eclipse下项目复制改名注意事项
  6. linux crontab定时任务运行shell脚本(shell执行sql文件)
  7. WildFly配置gzip压缩
  8. 枚举进程,线程,堆 CreateToolhelp32Snapshot
  9. 各ui库项目结构
  10. 打包成exe可执行文件的方法