首先要添加NPOI.dll程序集 https://yunpan.cn/cMeSTELJSXmJJ  访问密码 8d83

把里面的程序集都添加到引用里

下面的代码是从数据库导出到Excel

   public void DaoChuExcel(string path)         {  //path是导出的excel要保存的路径
using (Stream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{ //创建一个表
XSSFWorkbook work = new XSSFWorkbook();
//创建页
ISheet sheet = work.CreateSheet("zx");
List<MemberInfo> list = memDal.GetAllMemberByDelFlag();
//创建行
for (int i = ; i < list.Count; i++)
{
IRow row = sheet.CreateRow(i); //定义excel中的一行 //下面的Icell代表行中的单元格
ICell cell2 = row.CreateCell(, CellType.String);
cell2.SetCellValue(list[i].MemName); ICell cell3 = row.CreateCell(, CellType.String);
cell3.SetCellValue(list[i].MemMobilePhone); ICell cell4 = row.CreateCell(, CellType.String);
cell4.SetCellValue(list[i].MemAddress); ICell cell5 = row.CreateCell(, CellType.Numeric);
cell5.SetCellValue(list[i].MemType); ICell cell6 = row.CreateCell(, CellType.String);
cell6.SetCellValue(list[i].MemNum); ICell cell7 = row.CreateCell(, CellType.String);
cell7.SetCellValue(list[i].MemGender); ICell cell8 = row.CreateCell(, CellType.Numeric);
cell8.SetCellValue(list[i].MemDiscount); ICell cell9 = row.CreateCell(, CellType.Numeric);
cell9.SetCellValue(list[i].MemMoney); ICell cell10 = row.CreateCell(, CellType.Numeric);
cell10.SetCellValue(list[i].DelFlag); ICell cell11 = row.CreateCell(, CellType.String);
cell11.SetCellValue(list[i].SubTime.ToShortDateString()); ICell cell12 = row.CreateCell(, CellType.Numeric);
cell12.SetCellValue(list[i].MemIntegral); ICell cell13 = row.CreateCell(, CellType.String);
cell13.SetCellValue(list[i].MemEndTime.ToShortDateString()); ICell cell14 = row.CreateCell(, CellType.String);
cell14.SetCellValue(list[i].MemBirthday.ToShortDateString());
}
work.Write(fs); //最后把表写入IO流中 } }

下面是从excel中导入数据库

  public void ReadExcel(string path)
{
using (Stream fs = new FileStream(path,FileMode.Open,FileAccess.Read)) //从指定路径读入excel表
{
List<MemberInfo> list = new List<MemberInfo>();
//工作表
IWorkbook workbook = WorkbookFactory.Create(fs);
//获取页
ISheet sheet = workbook.GetSheetAt();
//遍历行
for (int i = ; i <=sheet.LastRowNum; i++)
{
MemberInfo mem = new MemberInfo();
//获取行
IRow row = sheet.GetRow(i); mem.MemName = row.GetCell().StringCellValue;
mem.MemMobilePhone = row.GetCell().StringCellValue;
mem.MemAddress = row.GetCell().StringCellValue;
mem.MemType = Convert.ToInt32(row.GetCell().NumericCellValue);
mem.MemNum = row.GetCell().StringCellValue;
mem.MemGender = row.GetCell().StringCellValue;
mem.MemDiscount = Convert.ToDouble(row.GetCell().NumericCellValue);
mem.MemMoney = row.GetCell().NumericCellValue;
mem.DelFlag = Convert.ToInt32(row.GetCell().NumericCellValue); mem.SubTime =Convert.ToDateTime(row.GetCell().StringCellValue);
mem.MemIntegral = Convert.ToInt32(row.GetCell().NumericCellValue);
mem.MemEndTime = Convert.ToDateTime(row.GetCell().StringCellValue);
mem.MemBirthday = Convert.ToDateTime(row.GetCell().StringCellValue);
list.Add(mem);
}
memDal.AddMemberInfo(list); //把数据体添加到数据库
} }

最新文章

  1. 淘宝分布式文件存储系统:TFS
  2. 2016-2017 ACM-ICPC Asia-Bangkok Regional Contest
  3. 【java报错】Could not instantiate listener
  4. wampserver2.6下UCenter1.6.0与UCenter Home2.0整合安装
  5. URAL题解—不断跟新中
  6. ##DAY6 UIScrollView
  7. Qt实战之开发CSDN下载助手 (2)
  8. jquery操作HTML5 的data-*的用法实例分享
  9. 完全卸载hadoop安装的组件(hdp版本)
  10. 单例模式、简单工厂模式、XML解析
  11. log4j:WARN Please initialize the log4j system properly解决办法
  12. Wpf TemplateBinding
  13. ML.NET 示例:多类分类之鸢尾花分类
  14. FCN 项目部分代码学习
  15. vi相关
  16. 第11月第11天 avplayer循环播放
  17. Linux indent命令
  18. Hadoop基准测试
  19. 获取 stoken 或者id MVC写法
  20. Entity Framework:“无法加载指定的元数据资源

热门文章

  1. openerp 产品图片的批量写入
  2. Linux系统管理技术手册——第6章 添加新用户
  3. HTML部分标签的含义
  4. 【Unity探究】物理碰撞实验
  5. net programming guid
  6. matlab 全部的随机数函数
  7. keil uVision4一些使用总结(汉字注释,C关键字等)
  8. Unity 弹出界面时屏蔽对3D场景的点击
  9. bzoj2427
  10. COJ 0046 20701除法