• Aspose------导入Excel


    代码:

    public List<T> ImportExcelToList<T>()
            {
                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<T>(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<T> GetList<T>(DataTable table)
            {
                List<T> list = new List<T>();
                T t = default(T);
                PropertyInfo[] propertypes = null;
                string tempName = string.Empty;
                foreach (DataRow row in table.Rows)
                {
                    t = Activator.CreateInstance<T>();
                    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<ProjectItem> AddItemToTree(List<MergeItem> list)
            {
                var datalist = new List<ProjectItem>();
                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;
            }
  • 相关阅读:
    关于TextField
    判断一个显示对象是否移除
    不争气的Discuz!NT 3.6和MVC3整合,主要实现同步登录和注册,登出。
    我的博客是英文的
    TFS不提供 Team Foundation 服务的解决办法。
    四 为提高entity framework 性能,要注意哪些事情.
    三 EF 和ado.net 的性能对比.
    一 关于大项目的经验总结
    在.net 中,ajax 如何调用本页数据源
    关于有序guid 的使用
  • 原文地址:https://www.cnblogs.com/tianhengblogs/p/7712665.html
Copyright © 2020-2023  润新知