• NPOI兼容 excel2003,2007版本


    根据项目需要,需要对excel进行导入导出,所以选择NPOI,优点在这里就不详细介绍了,下面进入正题。

     1 public int Import(string path)
     2         {
     3              IList<Student> list = new List<Student>();
     4 
     5             try
     6             {
     7                 string strFileName = path;
     8                 using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
     9                 {
    10 
    11                     IWorkbook workbook = WorkbookFactory.Create(file);////使用接口,自动识别excel2003/2007格式主要就是这句话
    12                     
    13                         //获取所有的工作簿
    14                         for (int i = 0; i < workbook.NumberOfSheets; i++)
    15                         {
    16                             //获取所有的工作表
    17                             ISheet st = workbook.GetSheetAt(i);
    18                             
    19                                 //读取每一行
    20                                 for (int k = 1; k < st.LastRowNum + 1; k++)
    21                                 {
    22                                     //获取当前行
    23                                     IRow r = st.GetRow(k);
    24 
    25                                     if (r != null)
    26                                     {
    27 
    28                                         if (r.GetCell(0) != null)
    29                                         {
    30                                             //if (r.GetCell(0).ToString().Length == 8)//验证考号是不是9位
    31                                             //{
    32                                             MarkingSchool.Model.Student stu = new MarkingSchool.Model.Student();
    33                                             if (r.GetCell(0) != null)
    34                                             {
    35                                                 stu.studentCode = r.GetCell(0).ToString();
    36                                             }
    37                                             if (r.GetCell(1) != null)
    38                                             {
    39                                                 stu.studentName = r.GetCell(1).ToString();
    40 
    41                                             }
    42                                             if (r.GetCell(2) != null) stu.schoolCode = r.GetCell(2).ToString();
    43                                             if (r.GetCell(3) != null) stu.gradeCode = r.GetCell(3).ToString();
    44                                             if (r.GetCell(4) != null) stu.classesCode = r.GetCell(4).ToString();
    45 
    46                                             if (r.GetCell(5) != null) stu.period = r.GetCell(5).ToString();
    47                                             if (r.GetCell(6) != null) stu.nationChineseIdentifyCode = r.GetCell(6).ToString();
    48                                             if (r.GetCell(7) != null) stu.scienceOrArtIdentifyCode = r.GetCell(7).ToString();
    49 
    50                                             list.Add(stu);
    51                                             //}
    52                                         }
    53 
    54                                     }
    55                                 }
    56 
    57                             
    58                         }
    59                     
    60                 }
    View Code

    以上是同excel导入到list,Studnet为学生实体类 ,然后通过通过Student在导入数据库,

    也可以通过Bulk Insert批量插入 。

    Bulk Insert 简介:http://blog.chinaunix.net/uid-12427199-id-3413752.html

    npoi官方最新下载地址:http://npoi.codeplex.com

    Npoi 学习系列教程推荐:http://tonyqus.sinaapp.com

  • 相关阅读:
    django修身大法之一阳指
    django修身大法之归天心法
    事务(转账功能)
    el技术,,,,,JSTL技术,,,,,javaEE的开发模式
    DBUtils
    Jquery的Ajax
    实例_一个CSS开门动画
    js_快速判断浏览器是否是IE9以下版本浏览器
    html5_通过网页添加QQ或者加入QQ群
    html5_video&audio的autoplay属性失效的解决方法
  • 原文地址:https://www.cnblogs.com/ps903942560/p/4607307.html
Copyright © 2020-2023  润新知