#region 从Excel导入 //用法 //var cellHeader = new Dictionary(); //cellHeader.Add("dpdm","中文"); // cellHeader.Add("dpmc", "中文"); // cellHeader.Add("xz", "xz"); // cellHeader.Add("qy", "qy"); // cellHeader.Add("spdm", "spdm"); // cellHeader.Add("dl", "dl"); // cellHeader.Add("dpj", "dpj"); // cellHeader.Add("ys", "ys"); // cellHeader.Add("cm", "cm"); // cellHeader.Add("sl", "sl"); // cellHeader.Add("bzje", "bzje"); // cellHeader.Add("dhje", "dhje"); // var errorMsg = new StringBuilder(); //var list = ExcelHelper.ExcelToEntityList(cellHeader, fileName, out errorMsg); ////// 从Excel取数据并记录到List集合里 /// /// 单元头的值和名称:{ { "UserName", "姓名" }, { "Age", "年龄" } }; /// 保存文件绝对路径 /// 错误信息 /// 转换后的List对象集合 public static List ExcelToEntityList (Dictionary<string, string> cellHeard, string filePath, out StringBuilder errorMsg) where T : new() { List enlist = new List (); errorMsg = new StringBuilder(); try { if (Regex.IsMatch(filePath, ".xls$")) // 2003 { enlist = Excel2003ToEntityList (cellHeard, filePath, out errorMsg); } else if (Regex.IsMatch(filePath, ".xlsx$")) // 2007 { //return FailureResultMsg("请选择Excel文件"); // 未设计 } return enlist; } catch (Exception ex) { throw ex; } } /// /// 从Excel2003取数据并记录到List集合里 /// /// 单元头的Key和Value:{ { "UserName", "姓名" }, { "Age", "年龄" } }; /// 保存文件绝对路径 /// 错误信息 /// 转换好的List对象集合 private static List Excel2003ToEntityList (Dictionary<string, string> cellHeard, string filePath, out StringBuilder errorMsg) where T : new() { errorMsg = new StringBuilder(); // 错误信息,Excel转换到实体对象时,会有格式的错误信息 List enlist = new List (); // 转换后的集合 List<string> keys = cellHeard.Keys.ToList(); // 要赋值的实体对象属性名称 try { using (FileStream fs = File.OpenRead(filePath)) { HSSFWorkbook workbook = new HSSFWorkbook(fs); HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0); // 获取此文件第一个Sheet页 for (int i = 1; i <= sheet.PhysicalNumberOfRows; i++) // 从1开始,第0行为单元头 { if (i >= 289) { } // 1.判断当前行是否空行,若空行就不在进行读取下一行操作,结束Excel读取操作 if (sheet.GetRow(i) == null) { break; } //var cells = sheet.GetRow(i).Cells; //var flag = false; //for(int t=0;t //{ // //var a = sheet.GetRow(i).GetCell(t); // // if (!string.IsNullOrEmpty(sheet.GetRow(i).GetCell(t).ToString())) // // { // // flag = true; // // } //} //if (!flag) //{ // break; //} T en = new T(); string errStr = ""; // 当前行转换时,是否有错误信息,格式为:第1行数据转换异常:XXX列; for (int j = 0; j < keys.Count; j++) { // 2.若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类,eg:UserEn.TrueName if (keys[j].IndexOf(".") >= 0) { // 2.1解析子类属性 string[] properotyArray = keys[j].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries); string subClassName = properotyArray[0]; // '.'前面的为子类的名称 string subClassProperotyName = properotyArray[1]; // '.'后面的为子类的属性名称 System.Reflection.PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName); // 获取子类的类型 if (subClassInfo != null) { // 2.1.1 获取子类的实例 var subClassEn = en.GetType().GetProperty(subClassName).GetValue(en, null); // 2.1.2 根据属性名称获取子类里的属性信息 System.Reflection.PropertyInfo properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName); if (properotyInfo != null) { try { // Excel单元格的值转换为对象属性的值,若类型不对,记录出错信息 properotyInfo.SetValue(subClassEn, GetExcelCellToProperty(properotyInfo.PropertyType, sheet.GetRow(i).GetCell(j), errStr).Item1, null); } catch (Exception e) { if (errStr.Length == 0) { errStr = "第" + i + "行数据转换异常:"; } errStr += cellHeard[keys[j]] + "列;"; } } } } else { // 3.给指定的属性赋值 System.Reflection.PropertyInfo properotyInfo = en.GetType().GetProperty(keys[j]); if (properotyInfo != null) { try { if (GetExcelCellToProperty(properotyInfo.PropertyType, sheet.GetRow(i).GetCell(j), errStr).Item2.Length != 0) { errStr += cellHeard[keys[j]] + "列"; } // Excel单元格的值转换为对象属性的值,若类型不对,记录出错信息 properotyInfo.SetValue(en, GetExcelCellToProperty(properotyInfo.PropertyType, sheet.GetRow(i).GetCell(j), errStr).Item1, null); } catch (Exception e) { if (errStr.Length == 0) { errStr = "第" + i + "行数据转换异常:"; } errStr += cellHeard[keys[j]] + "列;"; } } } } // 若有错误信息,就添加到错误信息里 if (errStr.Length > 0) { errorMsg.AppendLine(errStr); } enlist.Add(en); } } return enlist; } catch (Exception ex) { throw ex; } } /// > GetExcelCellToProperty(Type distanceType, ICell sourceCell, string errStr) { object rs = distanceType.IsValueType ? Activator.CreateInstance(distanceType) : null; // 1.判断传递的单元格是否为空 if (sourceCell == null || string.IsNullOrEmpty(sourceCell.ToString())) { return new Tuple/// 从Excel获取值传递到对象的属性里 /// /// 目标对象类型 /// 对象属性的值 private static Tuple
////// 导出Excel /// /// /// 英文中文列名对照 /// 保存路径 public static void getExcel(List lists, Dictionary head, string workbookFile) { try { XSSFWorkbook workbook = new XSSFWorkbook(); using (MemoryStream ms = new MemoryStream()) { var sheet = workbook.CreateSheet(); var headerRow = sheet.CreateRow(0); bool h = false; int j = 1; Type type = typeof(T); PropertyInfo[] properties = type.GetProperties(); foreach (T item in lists) { var dataRow = sheet.CreateRow(j); int i = 0; foreach (PropertyInfo column in properties) { if (!h) { if (head.Keys.Contains(column.Name)) { headerRow.CreateCell(i).SetCellValue(head[column.Name] == null ? column.Name : head[column.Name].ToString()); dataRow.CreateCell(i).SetCellValue(column.GetValue(item, null) == null ? "" : column.GetValue(item, null).ToString()); } else { i -= 1; } } else { if (head.Keys.Contains(column.Name)) { dataRow.CreateCell(i).SetCellValue(column.GetValue(item, null) == null ? "" : column.GetValue(item, null).ToString()); } else { i -= 1; } } i++; } h = true; j++; } workbook.Write(ms); using (FileStream fs = new FileStream(workbookFile, FileMode.Create, FileAccess.Write)) { byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); } sheet = null; headerRow = null; workbook = null; } } catch (Exception ee) { string see = ee.Message; } }