博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Aspose------导入Excel
阅读量:4708 次
发布时间:2019-06-10

本文共 5908 字,大约阅读时间需要 19 分钟。

代码:

public List
ImportExcelToList
() { HttpContext context = HttpContext.Current; if (context.Request.Files.AllKeys.Length > 0) { var key = context.Request.Files.AllKeys[0]; var file = context.Request.Files[key]; var folderName = Path.GetFileNameWithoutExtension(file.FileName); var fileExtension = Path.GetExtension(file.FileName); Workbook book = new Workbook(file.InputStream); Worksheet sheet = book.Worksheets[0]; Cells cells = sheet.Cells; var da = cells[0, 1].Value; var dataTable = cells.ExportDataTable(0, 0, cells.MaxDataRow + 1, cells.MaxColumn, true); var datalist = GetList
(dataTable); //拆分单元格后需要给Component为空的单元格赋值 Type type = typeof(T); PropertyInfo[] property = type.GetProperties(); for (var i = 0; i < datalist.Count(); i++) { var li = datalist[i]; foreach (var pi in property) { if (pi.Name == "Component") { var value = pi.GetValue(li); if (value == null) { var v = pi.GetValue(datalist[i - 1]); pi.SetValue(li, v); } } } } return datalist; } return null; } public List
GetList
(DataTable table) { List
list = new List
(); T t = default(T); PropertyInfo[] propertypes = null; string tempName = string.Empty; foreach (DataRow row in table.Rows) { t = Activator.CreateInstance
(); propertypes = t.GetType().GetProperties(); foreach (PropertyInfo pro in propertypes) { tempName = pro.Name; if (table.Columns.Contains(tempName)) { object value = row[tempName]; if (!value.ToString().Equals("")) { if (pro.Name == "NewDuct" || pro.Name == "ExistingDuct" || pro.Name == "NewAerial" || pro.Name == "ExistingAerial") { var v = ChangePropertyType(pro.PropertyType,value); pro.SetValue(t, v, null); continue; } pro.SetValue(t, value, null); } } } list.Add(t); } return list.Count == 0 ? null : list; } private object ChangePropertyType(Type type, object value) { object data; if (type.FullName.ToLower().Contains("decimal")) { data = 0; data = Convert.ToDecimal(value); } else { data = ""; data = Convert.ToString(value); } return data; }

转成树结构

private List
AddItemToTree(List
list) { var datalist = new List
(); var component = ""; PropertyInfo[] property = typeof(MergeItem).GetProperties(); foreach (var li in list) { var item = new ProjectItem(); if (string.IsNullOrEmpty(li.SubComponent)) { component = li.Component; item.SubComponent = null; item.ItemLevel = "Level1"; item.Unit = null; } else { item.SubComponent = li.Component; item.ItemLevel = "Level2"; item.Unit = null; //建第三级 foreach (var pi in property) { var scenario = ""; if (pi.Name == "NewDuct") { scenario = "New Duct"; } else if (pi.Name == "ExistingDuct") { scenario = "Existing Duct"; } else if (pi.Name == "NewAerial") { scenario = "New Aerial"; } else if (pi.Name == "ExistingAerial") { scenario = "Existing Aerial"; } else { continue; } var value = pi.GetValue(li); if (value != null) { var child = new ProjectItem(); child.Component = component; child.SubComponent = item.SubComponent; child.Description = li.SubComponent; child.Unit = li.Unit; child.Scenario = scenario; child.UnitCost = Convert.ToDecimal(value); child.Remark = li.Remark; child.FillInstruct = li.FillInstruct; child.ItemLevel = "Level3"; datalist.Add(child); } } } item.Component = component; item.Description = li.Component; item.Scenario = null; item.UnitCost = null; item.Remark = null; item.FillInstruct = null; var isExist = datalist.Any(p => p.Component == item.Component && p.SubComponent == item.SubComponent && p.Description == item.Description && p.ItemLevel == "Level2"); if (!isExist) { datalist.Add(item); } } return datalist; }

 

转载于:https://www.cnblogs.com/tianhengblogs/p/7712665.html

你可能感兴趣的文章
Vue疑难杂症
查看>>
spring boot 错误处理之深度历险
查看>>
MySQL对于有大量重复数据表的处理方法
查看>>
Android应用开发学习笔记之多线程与Handler消息处理机制
查看>>
ubuntu 设置环境变量
查看>>
JSTL详解(一)
查看>>
Manacher 算法
查看>>
Linux磁盘及文件系统(三)Linux文件系统
查看>>
SDWebImage源码阅读(二)NSData+ImageContentType
查看>>
别在最好的年纪辜负最好的自己
查看>>
微软品牌形象广告 不是一般的优秀
查看>>
【Object.prototype.toString.call()】---判断某个对象属于哪种内置类型------【巷子】...
查看>>
转载:结构体的字节对齐
查看>>
用github来展示你的前端页面吧
查看>>
内存池
查看>>
SQLServer到底支持多少连接数的并发?
查看>>
深入分析java中文乱码问题
查看>>
Nginx(二)
查看>>
CF #329 D
查看>>
Android中pendingIntent的深入理解
查看>>