Esempio n. 1
0
        /// <summary>
        /// 添加设备(添加前先查询是否已存在该设备编号,如果没有则添加,有则返回添加失败)
        /// </summary>
        /// <param name="deviceNo">设备编号</param>
        /// <param name="remark">设备描述</param>
        /// <returns></returns>
        public async Task <bool> AddDevice(string deviceNo, string deviceDescribe)
        {
            var task = new Task <bool>(() =>
            {
                var info = this._deviceInfo.GetAll().Where(d => d.DeviceNo == deviceNo).FirstOrDefault();
                if (info != null)
                {
                    return(false);
                }
                Device_Info model = new Device_Info
                {
                    DeviceNo       = deviceNo,
                    DeviceDescribe = deviceDescribe,
                    DeviceStatus   = 0,
                };
                this._deviceInfo.InsertAsync(model);
                return(true);
            });

            task.Start();
            return(await task);
        }
        /// <summary>
        /// 通过NPOI将Excel文件中的设备导入到数据库
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="suffix">文件后缀名</param>
        /// <returns>true:有设备导入, false:无设备导入</returns>
        private async Task <bool> GetExcelData(string filePath, string suffix)
        {
            var task = new Task <bool>(() =>
            {
                bool flag = false;
                using (FileStream fs = System.IO.File.OpenRead(filePath))
                {
                    IWorkbook wk;
                    if (suffix == ".xlsx")
                    {
                        wk = new XSSFWorkbook(fs);
                    }
                    else
                    {
                        wk = new HSSFWorkbook(fs);
                    }
                    int insertCount = 0;
                    if (wk.NumberOfSheets > 0)
                    {
                        ISheet sheet = wk.GetSheetAt(0);    //读取excel第一张表格
                        int rows     = sheet.LastRowNum;
                        if (rows < 1)
                        {
                            return(false);
                        }
                        for (int i = 1; i <= rows; i++)
                        {
                            //提取表格数据
                            IRow row        = sheet.GetRow(i);
                            string deviceNo = row.GetCell(0).ToString();
                            if (string.IsNullOrWhiteSpace(deviceNo) || deviceNo.Length > 9)
                            {
                                continue;
                            }
                            string deviceDescribe = row.GetCell(1).ToString();
                            //插入数据库操作
                            var model = this._deviceInfo.GetAll().Where(d => d.DeviceNo == deviceNo).FirstOrDefault();
                            if (model == null)
                            {
                                //没有该设备号时才导入
                                Device_Info d = new Device_Info
                                {
                                    DeviceNo       = deviceNo,
                                    DeviceDescribe = deviceDescribe,
                                    DeviceStatus   = 0,
                                };
                                this._deviceInfo.InsertAsync(d);
                                insertCount++;
                            }
                        }
                        ;
                    }
                    if (insertCount <= 0)
                    {
                        flag = false;
                    }
                    else
                    {
                        flag = true;
                    }
                }
                return(flag);
            });

            task.Start();
            return(await task);
        }