注:对于实体类对象最好新建一个并且继承原有实体类,这样可以将类型进行修改;

方法一:此种方法是用EPPLUS中的FileInfo流进行读取的(是不是流我还真不太了解,若有懂得请留言,非常感谢了)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using Abp.Extensions;
 7
 8 namespace HYZT.Ltxy.International.Ctrip.Exporting
 9 {
10     public class ExcelLib
11     {
12         public  ICtripPolicyExcelImport GetExcel(string filePath)
13         {
14             if (filePath.Trim() .IsNullOrEmpty())
15                 throw new Exception("文件名不能为空");
16     //因为这儿用得是EPPLUS对Excel进行的操作,所以只能操作
17     //2007以后的版本以后的(即扩展名为.xlsx)
18             if (!filePath.Trim().EndsWith("xlsx"))
19                 throw new Exception("请使用office Excel 2007版本或2010版本");
20
21             else if (filePath.Trim().EndsWith("xlsx"))
22             {
23                 ICtripPolicyExcelImport res = new CtripPolicyExcelImport(filePath.Trim());
24                 return res;
25             }
26             else return null;
27         }
28     }
29 }

方法接口:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace HYZT.Ltxy.International.Ctrip.Exporting
 8 {
 9     public interface ICtripPolicyExcelImport
10     {
11         /// <summary> 打开文件 </summary>
12         bool Open();
13         //ExcelVersion Version { get; }
14         /// <summary> 文件路径 </summary>
15         string FilePath { get; set; }
16         /// <summary> 文件是否已经打开 </summary>
17         bool IfOpen { get; }
18         /// <summary> 文件包含工作表的数量 </summary>
19         int SheetCount { get; }
20         /// <summary> 当前工作表序号 </summary>
21         int CurrentSheetIndex { get; set; }
22         /// <summary> 获取当前工作表中行数 </summary>
23         int GetRowCount();
24         /// <summary> 获取当前工作表中列数 </summary>
25         int GetColumnCount();
26         /// <summary> 获取当前工作表中某一行中单元格的数量 </summary>
27         /// <param name="Row">行序号</param>
28         int GetCellCountInRow(int Row);
29         /// <summary> 获取当前工作表中某一单元格的值(按字符串返回) </summary>
30         /// <param name="Row">行序号</param>
31         /// <param name="Col">列序号</param>
32         string GetCellValue(int Row, int Col);
33         /// <summary> 关闭文件 </summary>
34         void Close();
35     }
36 }

方法实现:

  1 using OfficeOpenXml;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.IO;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8
  9 namespace HYZT.Ltxy.International.Ctrip.Exporting
 10 {
 11     public class CtripPolicyExcelImport:ICtripPolicyExcelImport
 12     {
 13
 14         public CtripPolicyExcelImport()
 15         { }
 16
 17         public CtripPolicyExcelImport(string path)
 18         { filePath = path; }
 19
 20
 21         private string filePath = "";
 22         private ExcelWorkbook book = null;
 23         private int sheetCount = 0;
 24         private bool ifOpen = false;
 25         private int currentSheetIndex = 0;
 26         private ExcelWorksheet currentSheet = null;
 27         private ExcelPackage ep = null;
 28
 29          public bool Open()
 30         {
 31             try
 32             {
 33                 ep = new ExcelPackage(new FileInfo(filePath));
 34
 35                 if (ep == null) return false;
 36                 book =ep.Workbook;
 37                 sheetCount = book.Worksheets.Count;
 38                 currentSheetIndex = 0;
 39                 currentSheet = book.Worksheets[1];
 40                 ifOpen = true;
 41             }
 42             catch (Exception ex)
 43             {
 44                 throw new Exception(ex.Message);
 45             }
 46             return true;
 47         }
 48
 49         public void Close()
 50         {
 51             if (!ifOpen || ep == null) return;
 52             ep.Dispose();
 53         }
 54
 55         //public ExcelVersion Version
 56         //{ get { return ExcelVersion.Excel07; } }
 57
 58         public string FilePath
 59         {
 60             get { return filePath; }
 61             set { filePath = value; }
 62         }
 63
 64         public bool IfOpen
 65         { get { return ifOpen; } }
 66
 67         public int SheetCount
 68         { get { return sheetCount; } }
 69
 70         public int CurrentSheetIndex
 71         {
 72             get  { return currentSheetIndex; }
 73             set
 74             {
 75                 if (value != currentSheetIndex)
 76                 {
 77                     if (value >= sheetCount)
 78                         throw new Exception("工作表序号超出范围");
 79                     currentSheetIndex = value;
 80                     currentSheet =book.Worksheets[currentSheetIndex+1];
 81                 }
 82             }
 83         }
 84
 85         public int GetRowCount()
 86         {
 87             if (currentSheet == null) return 0;
 88             return currentSheet.Dimension.End.Row;
 89         }
 90
 91         public int GetColumnCount()
 92         {
 93             if (currentSheet == null) return 0;
 94             return currentSheet.Dimension.End.Column;
 95         }
 96
 97         public int GetCellCountInRow(int Row)
 98         {
 99             if (currentSheet == null) return 0;
100             if (Row >= currentSheet.Dimension.End.Row) return 0;
101             return currentSheet.Dimension.End.Column;
102         }
103     //根据行号和列号获取指定单元格的数据
104         public string GetCellValue(int Row, int Col)
105         {
106             if (currentSheet == null) return "";
107             if (Row >= currentSheet.Dimension.End.Row || Col >= currentSheet.Dimension.End.Column) return "";
108             object tmpO =currentSheet.GetValue(Row+1, Col+1);
109             if (tmpO == null) return "";
110             return tmpO.ToString();
111         }
112     }
113 }

方法调用实现功能:

 1 //用于程序是在本地,所以此时的路径是本地电脑的绝对路劲;
 2 //当程序发布后此路径应该是服务器上的绝对路径,所以在此之前还要有
 3 //一项功能是将本地文件上传到服务器上的指定位置,此时在获取路径即可
 4  public string GetExcelToCtripPolicy(string filePath)
 5         {
 6             ExcelLib lib = new ExcelLib();
 7             if (filePath == null)
 8                 return new ReturnResult<bool>(false, "未找到相应文件");
 9            string str= tmp.GetCellValue(i, j);
10            return str;
11         }

方法二:将Excel表格转化成DataTable表,然后在对DataTable进行业务操作

 1 using Abp.Application.Services;
 2 using OfficeOpenXml;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Data;
 6 using System.IO;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10
11 namespace HYZT.Ltxy.International.Ctrip.GetExcelToDataTable
12 {
13     public class EPPlusHelperAppService:ApplicationService,IEPPlusHelperAppService
14     {
15         private   static string GetString(object obj)
16         {
17             try
18             {
19                 return obj.ToString();
20             }
21             catch (Exception ex)
22             {
23                 return "";
24             }
25         }
26
27         /// <summary>
28         ///将指定的Excel的文件转换成DataTable (Excel的第一个sheet)
29         /// </summary>
30         /// <param name="fullFielPath">文件的绝对路径</param>
31         /// <returns></returns>
32         public DataTable WorksheetToTable(string filePath)
33         {
34             try
35             {
36                 FileInfo existingFile = new FileInfo(filePath);
37
38                 ExcelPackage package = new ExcelPackage(existingFile);
39                 ExcelWorksheet worksheet = package.Workbook.Worksheets[1];//选定 指定页
40
41                 return WorksheetToTable(worksheet);
42             }
43             catch (Exception)
44             {
45                 throw;
46             }
47         }
48
49         /// <summary>
50         /// 将worksheet转成datatable
51         /// </summary>
52         /// <param name="worksheet">待处理的worksheet</param>
53         /// <returns>返回处理后的datatable</returns>
54         public  static  DataTable WorksheetToTable(ExcelWorksheet worksheet)
55         {
56             //获取worksheet的行数
57             int rows = worksheet.Dimension.End.Row;
58             //获取worksheet的列数
59             int cols = worksheet.Dimension.End.Column;
60
61             DataTable dt = new DataTable(worksheet.Name);
62             DataRow dr = null;
63             for (int i = 1; i <= rows; i++)
64             {
65                 if (i > 1)
66                     dr = dt.Rows.Add();
67
68                 for (int j = 1; j <= cols; j++)
69                 {
70                     //默认将第一行设置为datatable的标题
71                     if (i == 1)
72                         dt.Columns.Add(GetString(worksheet.Cells[i, j].Value));
73                     //剩下的写入datatable
74                     else
75                         dr[j - 1] = GetString(worksheet.Cells[i, j].Value);
76                 }
77             }
78             return dt;
79         }
80     }
81 }

之前我有一个程序用的是方法一进行Excel导入的,速度不是很快,后来我又用了第二种方法但是速度更慢了,到底这两种方法哪种快,请大虾指导,还是我用第二种方法的时候业务判断有问题,不得而知,

就请明白人指导我到底这两种方法哪种比较好些;

3:实体类与DataTable之间的互转:

  1 /// <summary>
  2     /// DataTable与实体类互相转换
  3     /// </summary>
  4     /// <typeparam name="T">实体类</typeparam>
  5     public class ModelHandler<T> where T : new()
  6     {
  7         #region DataTable转换成实体类
  8
  9         /// <summary>
 10         /// 填充对象列表:用DataSet的第一个表填充实体类
 11         /// </summary>
 12         /// <param name="ds">DataSet</param>
 13         /// <returns></returns>
 14         public List<T> FillModel(DataSet ds)
 15         {
 16             if (ds == null || ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
 17             {
 18                 return null;
 19             }
 20             else
 21             {
 22                 return FillModel(ds.Tables[0]);
 23             }
 24         }
 25
 26         /// <summary>
 27         /// 填充对象列表:用DataSet的第index个表填充实体类
 28         /// </summary>
 29         public List<T> FillModel(DataSet ds, int index)
 30         {
 31             if (ds == null || ds.Tables.Count <= index || ds.Tables[index].Rows.Count == 0)
 32             {
 33                 return null;
 34             }
 35             else
 36             {
 37                 return FillModel(ds.Tables[index]);
 38             }
 39         }
 40
 41         /// <summary>
 42         /// 填充对象列表:用DataTable填充实体类
 43         /// </summary>
 44         public List<T> FillModel(DataTable dt)
 45         {
 46             if (dt == null || dt.Rows.Count == 0)
 47             {
 48                 return null;
 49             }
 50             List<T> modelList = new List<T>();
 51             foreach (DataRow dr in dt.Rows)
 52             {
 53                 //T model = (T)Activator.CreateInstance(typeof(T));
 54                 T model = new T();
 55                 for (int i = 0; i < dr.Table.Columns.Count; i++)
 56                 {
 57                     PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
 58                     if (propertyInfo != null && dr[i] != DBNull.Value)
 59                         propertyInfo.SetValue(model, dr[i], null);
 60                 }
 61
 62                 modelList.Add(model);
 63             }
 64             return modelList;
 65         }
 66
 67         /// <summary>
 68         /// 填充对象:用DataRow填充实体类
 69         /// </summary>
 70         public T FillModel(DataRow dr)
 71         {
 72             if (dr == null)
 73             {
 74                 return default(T);
 75             }
 76
 77             //T model = (T)Activator.CreateInstance(typeof(T));
 78             T model = new T();
 79
 80             for (int i = 0; i < dr.Table.Columns.Count; i++)
 81             {
 82                 PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
 83                 if (propertyInfo != null && dr[i] != DBNull.Value)
 84                     propertyInfo.SetValue(model,dr[i],null);
 85             }
 86             return model;
 87         }
 88
 89         #endregion
 90
 91         #region 实体类转换成DataTable
 92
 93         /// <summary>
 94         /// 实体类转换成DataSet
 95         /// </summary>
 96         /// <param name="modelList">实体类列表</param>
 97         /// <returns></returns>
 98         public DataSet FillDataSet(List<T> modelList)
 99         {
100             if (modelList == null || modelList.Count == 0)
101             {
102                 return null;
103             }
104             else
105             {
106                 DataSet ds = new DataSet();
107                 ds.Tables.Add(FillDataTable(modelList));
108                 return ds;
109             }
110         }
111
112         /// <summary>
113         /// 实体类转换成DataTable
114         /// </summary>
115         /// <param name="modelList">实体类列表</param>
116         /// <returns></returns>
117         public DataTable FillDataTable(List<T> modelList)
118         {
119             if (modelList == null || modelList.Count == 0)
120             {
121                 return null;
122             }
123             DataTable dt = CreateData(modelList[0]);
124
125             foreach(T model in modelList)
126             {
127                 DataRow dataRow = dt.NewRow();
128                 foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
129                 {
130                     dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
131                 }
132                 dt.Rows.Add(dataRow);
133             }
134             return dt;
135         }
136
137         /// <summary>
138         /// 根据实体类得到表结构
139         /// </summary>
140         /// <param name="model">实体类</param>
141         /// <returns></returns>
142         private DataTable CreateData(T model)
143         {
144             DataTable dataTable = new DataTable(typeof (T).Name);
145             foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
146             {
147                 dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
148             }
149             return dataTable;
150         }
151
152         #endregion
153     } 

3.1:将实体类转化成DataTable之后对DataTable进行操作

 
//首先将数据库中查出的数据变成实体类集合,然后将实体类集合转变成DataTable表格
//dataPercent,然后在对此表格进行操作,表头转化和表格信息//设置新表的表头:即字段名,有英文改为中文 1 for (int i = 0; i < dataPercent.Columns.Count; i++)
 2                 {
 3                     DataColumn column = dataPercent.Columns[i];
 4                     string name = column.ColumnName;
 5                     switch (name)
 6                     {
 7                         case "IsDomestic":
 8                             dataPercent.Columns[i].ColumnName = "国内/国际";
 9                             break;
10                         case "TripType":
11                             dataPercent.Columns[i].ColumnName = "行程类型";
12                             break;
13                         case "GoFlightCode":
14                             dataPercent.Columns[i].ColumnName = "去程航班号";
15                             break;
16                         case "GoCabin":
17                             dataPercent.Columns[i].ColumnName = "去程舱位";
18                             break;
19                         case "GoSeatNum":
20                             dataPercent.Columns[i].ColumnName = "去程座位数";
21                             break;
22                         case "Line":
23                             dataPercent.Columns[i].ColumnName = "去程行程";
24                             break;
25                         case "DepartDate":
26                             dataPercent.Columns[i].ColumnName = "去程航班日期";
27                             break;
28                         case "BackFlightCode":
29                             dataPercent.Columns[i].ColumnName = "回程航班号";
30                             break;
31                         case "BackCabin":
32                             dataPercent.Columns[i].ColumnName = "回程舱位";
33                             break;
34                         case "ReturnDate":
35                             dataPercent.Columns[i].ColumnName = "回程航班日期";
36                             break;
37                         case "BackSeatNum":
38                             dataPercent.Columns[i].ColumnName = "回程座位数";
39                             break;
40                         case "AvCmd":
41                             dataPercent.Columns[i].ColumnName = "黑屏的AV查询指令";
42                             break;
43                         case "State":
44                             dataPercent.Columns[i].ColumnName = "状态";
45                             break;
46                         case "Interval":
47                             dataPercent.Columns[i].ColumnName = "间隔时间(分钟)";
48                             break;
49                         case "Telphone":
50                             dataPercent.Columns[i].ColumnName = "联系电话";
51                             break;
52                         case "Remark":
53                             dataPercent.Columns[i].ColumnName = "备注";
54                             break;
55                     }
56                 }
57                 DataTable dtResult = new DataTable();
58                 //克隆表结构
59                 dtResult = dataPercent.Clone();           //将克隆的表格进行字段类型的重置,有利于改变表格数据
60                 foreach (DataColumn col in dtResult.Columns)
61                 {
62                     if (col.ColumnName == "行程类型" || col.ColumnName == "国内/国际" ||col.ColumnName =="状态")
63                     {
64                         //修改列类型
65                         col.DataType = typeof(String);
66                     }
67                 }
68                 foreach (DataRow row in dataPercent.Rows)
69                 {
70                     DataRow rowNew = dtResult.NewRow();
71                     //rowNew["Id"] = row["Id"];
72                     rowNew["国内/国际"] = row["国内/国际"] == "true" ? "是" : "否";
73                     rowNew["行程类型"] = row["行程类型"] == "1" ? "单程" : "往返";
74                     rowNew["去程航班号"] = row["去程航班号"];
75                     rowNew["去程舱位"] = row["去程舱位"];
76                     rowNew["去程座位数"] = row["去程座位数"];
77                     rowNew["去程行程"] = row["去程行程"];
78                     rowNew["去程航班日期"] = row["去程航班日期"];
79                     rowNew["回程航班号"] = row["回程航班号"];
80                     rowNew["回程舱位"] = row["回程舱位"];
81                     rowNew["回程航班日期"] = row["回程航班日期"];
82                     rowNew["回程座位数"] = row["回程座位数"];
83                     rowNew["黑屏的AV查询指令"] = row["黑屏的AV查询指令"];
84                     //rowNew["创建人Id"] = row["创建人Id"];
85                     rowNew["状态"] = row["状态"] == "1" ?  "有效" : "挂起";
86                     rowNew["间隔时间(分钟)"] = row["间隔时间(分钟)"];
87                     rowNew["联系电话"] = row["联系电话"];
88                     rowNew["备注"] = row["备注"];
89                     dtResult.Rows.Add(rowNew);
90                 }

转载于:https://www.cnblogs.com/lubolin/p/6594305.html

C#导入导出Excele数据相关推荐

  1. MongoDB 教程六: MongoDB管理:数据导入导出,数据备份恢复及用户安全与认证

    视频地址:MongoDB 教程六: MongoDB管理:数据导入导出,数据备份恢复及用户安全与认证 MongoDB数据库备份与恢复 一.备份 先介绍下命令语法: mongodump -h dbhost ...

  2. toad导入数据_Oracle 使用TOAD实现导入导出Excel数据

    在Oracle应用程序的开发过程中,访问数据库对象和编写SQL程序是一件乏味且耗费时间的工作,对数据库进行日常管理也是需要很多SQL脚本才能完成的.Quest Software为此提供了高效的Orac ...

  3. 如何完美解决Sqoop导入导出MySQL数据错位问题

    我发现小伙伴们在使用Sqoop把数据从MySQL导入到Hive的过程中经常会遇到数据错位的问题,虽然最后都是通过添加参数的方法来解决这个问题,但是我认为这并不是一个完美的解决方案,所以花了一点时间研究 ...

  4. java 导入excel工具类_java Excel工具类,导入导出Excel数据

    java Excel工具类,导入导出Excel数据,导入数据对合并表格有判断获取数据: 导出数据到Excel,Excel文件不存在会创建. 使用的是poi处理,兼容Excel. 对反射不够理解,目前先 ...

  5. windows 导oracle用户,windows下Oracle导入导出用户数据

    Oracle导入导出用户数据 -- 导出用户 -- 一.新建目录.目录名称:third,目录路径:E:\OracleUserCeshi create directory third as 'E:\Or ...

  6. python导入excel数据-Python数据处理之导入导出excel数据

    欢迎点击上方"AntDream"关注我 .Python的一大应用就是数据分析了,而数据分析中,经常碰到需要处理Excel数据的情况.这里做一个Python处理Excel数据的总结, ...

  7. toad mysql导入excel_Oracle 使用TOAD实现导入导出Excel数据

    在Oracle应用程序的开发过程中,访问数据库对象和编写SQL程序是一件乏味且耗费时间的工作,对数据库进行日常管理也是需要很多SQL脚本才能完成的.Quest Software为此提供了高效的Orac ...

  8. java excel data 导入数据_java实现导入导出excel数据

    项目需要,要实现一个导入导出excel的功能,于是,任务驱动着我学习到了POI和JXL这2个java操作Excel的插件. 一.POI和JXL介绍 1.POI:是对所有office资源进行读写的一套工 ...

  9. 5.非关系型数据库(Nosql)之mongodb:创建集合,备份与导入导出, 数据还原,导入导出

     1固定集合 固定集合值得是事先创建而且大小固定的集合 2固定集合的特征:固定集合很像环形队列,如果空间不足,最早文档就会被删除,为新的文档腾出空间.一般来说,固定集合适用于任何想要自动淘汰过期属 ...

最新文章

  1. Hibernate flush理解
  2. .NET基础示例系列之十六:制做进程监视器
  3. 聊聊rocketmq的ProducerImpl
  4. LIME算法:模型的可解释性(代码实现)
  5. 算法导论-MIT笔记
  6. php XML文件解释类
  7. VTK:二次抽取用法实战
  8. LeetCode : Number of Segments in a String
  9. .net平台调用(P/Invoking)查询网站
  10. java转integer_Java的Integer与int互转
  11. 转载 ADB logcat 过滤方法(抓取日志)
  12. 【Python实战】有趣的代码百里挑一:这款“水波特效”脚本送给你,绝版哦~(建议保留)
  13. PyCharm设置背景颜色为白色
  14. Ubuntu 安装坚果云
  15. 单目标应用:白鲸优化算法(Beluga whale optimization,BWO)优化双向长短时记忆BiLSTM的权值和阈值(提供MATLAB代码)
  16. 台达PLC伺服追剪程序,电子凸轮,全部源代码,PLC程序和触 摸屏程序,DVP15MC。
  17. 基于matlab山脊线,教你如何利用水文,分析提取山脊线山谷线
  18. 如何测linux传输文件的速度,如何测试linux服务器的上传下载速度
  19. 网店美工之你不知道的图片设计技巧
  20. Go+Python双剑合璧

热门文章

  1. WinSCP无法连接linux,而secureCRT却可以
  2. Java中的委托模式
  3. jquery基础知识(一)
  4. javascript继承的原理
  5. 模块化加载_前端模块化概述
  6. 快速开发框架介绍-懂你的RUOYI
  7. Unity5.x 依赖关系打包 AssetBundle 研究
  8. iOS程序员必须知道的Android要点
  9. VC++ Tab Control控件的使用
  10. java sql结果写入csv文件_java在处理大数据的时候一些小技巧