Пример #1
0
        /// <summary>
        /// 执行导入
        /// </summary>
        /// <returns></returns>
        public string ImpData_Done()
        {
            HttpFileCollection files = context.Request.Files;

            if (files.Count == 0)
            {
                return("err@请选择要导入的数据信息。");
            }

            string errInfo = "";

            string ext      = ".xls";
            string fileName = System.IO.Path.GetFileName(files[0].FileName);

            if (fileName.Contains(".xlsx"))
            {
                ext = ".xlsx";
            }


            //设置文件名
            string fileNewName = DateTime.Now.ToString("yyyyMMddHHmmssff") + ext;

            //文件存放路径
            string filePath = BP.Sys.SystemConfig.PathOfTemp + "\\" + fileNewName;

            files[0].SaveAs(filePath);

            //从excel里面获得数据表.
            DataTable dt = BP.DA.DBLoad.ReadExcelFileToDataTable(filePath);

            //删除临时文件
            System.IO.File.Delete(filePath);

            if (dt.Rows.Count == 0)
            {
                return("err@无导入的数据");
            }

            //获得entity.
            Entities ens = ClassFactory.GetEns(this.EnsName);
            Entity   en  = ens.GetNewEntity;

            if (en.PK.Equals("MyPK") == true)
            {
                return(this.ImpData_DoneMyPK(ens, dt));
            }

            if (en.IsNoEntity == false)
            {
                return("err@必须是EntityNo或者EntityMyPK实体,才能导入.");
            }

            string noColName   = ""; //实体列的编号名称.
            string nameColName = ""; //实体列的名字名称.

            Attr attr = en.EnMap.GetAttrByKey("No");

            noColName = attr.Desc; //
            BP.En.Map map        = en.EnMap;
            String    codeStruct = map.CodeStruct;

            attr        = map.GetAttrByKey("Name");
            nameColName = attr.Desc; //

            //定义属性.
            Attrs attrs = en.EnMap.Attrs;

            int impWay = this.GetRequestValInt("ImpWay");

            #region 清空方式导入.
            //清空方式导入.
            int    count       = 0; //导入的行数
            int    changeCount = 0; //更新的行数
            String successInfo = "";
            if (impWay == 0)
            {
                ens.ClearTable();
                foreach (DataRow dr in dt.Rows)
                {
                    string no   = dr[noColName].ToString();
                    string name = dr[nameColName].ToString();

                    //判断是否是自增序列,序列的格式
                    if (!DataType.IsNullOrEmpty(codeStruct))
                    {
                        no = no.PadLeft(System.Int32.Parse(codeStruct), '0');
                    }

                    EntityNoName myen = ens.GetNewEntity as EntityNoName;
                    myen.No = no;
                    if (myen.IsExits == true)
                    {
                        errInfo += "err@编号[" + no + "][" + name + "]重复.";
                        continue;
                    }

                    myen.Name = name;

                    en = ens.GetNewEntity;

                    //给实体赋值
                    errInfo += SetEntityAttrVal(no, dr, attrs, en, dt, 0);
                    count++;
                    successInfo += "&nbsp;&nbsp;<span>" + noColName + "为" + no + "," + nameColName + "为" + name + "的导入成功</span><br/>";
                }
            }

            #endregion 清空方式导入.

            #region 更新方式导入
            if (impWay == 1 || impWay == 2)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string no   = dr[noColName].ToString();
                    string name = dr[nameColName].ToString();
                    //判断是否是自增序列,序列的格式
                    if (!DataType.IsNullOrEmpty(codeStruct))
                    {
                        no = no.PadLeft(System.Int32.Parse(codeStruct), '0');
                    }
                    EntityNoName myen = ens.GetNewEntity as EntityNoName;
                    myen.No = no;
                    if (myen.IsExits == true)
                    {
                        //给实体赋值
                        errInfo += SetEntityAttrVal(no, dr, attrs, myen, dt, 1);
                        changeCount++;
                        successInfo += "&nbsp;&nbsp;<span>" + noColName + "为" + no + "," + nameColName + "为" + name + "的更新成功</span><br/>";
                        continue;
                    }
                    myen.Name = name;

                    //给实体赋值
                    errInfo += SetEntityAttrVal(no, dr, attrs, en, dt, 0);
                    count++;
                    successInfo += "&nbsp;&nbsp;<span>" + noColName + "为" + no + "," + nameColName + "为" + name + "的导入成功</span><br/>";
                }
            }
            #endregion

            return("errInfo=" + errInfo + "@Split" + "count=" + count + "@Split" + "successInfo=" + successInfo + "@Split" + "changeCount=" + changeCount);
        }
Пример #2
0
 public string GetItem1Context(EntityNoName en)
 {
     return(this.GetD1Context(en.No, en.Name));
 }