Пример #1
0
        bool IDataProvider.Validate()
        {
            string            path         = FileHelper.GetFillVersionByName("填报规则");
            string            validateFile = FileHelper.GetFillRuleFile("填报规则", "CCC", null, null);
            SHFYDataValidator validator    = new SHFYDataValidator(string.Format("{0}\\{1}", path, validateFile));

            validator.ReadValidateRule();
            int       needAddedColumn = 0, lastValueColumnIndex = 0;
            Hashtable specialParameters = FileHelper.Get3CSpecialParameter();

            CollectInfo(this.DataSourceFile, specialParameters, ref needAddedColumn, ref lastValueColumnIndex);
            return(Validate(this.DataSourceFile, validator, specialParameters, needAddedColumn, lastValueColumnIndex));
        }
Пример #2
0
        /// <summary>
        /// 数据验证
        /// </summary>
        /// <param name="value"></param>
        /// <param name="validator"></param>
        private bool Validate(string fileName, SHFYDataValidator validator, Hashtable specialParameters, int needAddedColumn, int lastValueColumnIndex)
        {
            bool      result       = true;
            TreeValue root         = InitTreeNode();
            Hashtable treeDir      = GetTreeDir(root);
            Hashtable columnHeader = new Hashtable();
            Hashtable startIndex   = new Hashtable();
            string    destFile     = string.Format("{0}\\{1}_{2}.xlsx", Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName), DateTime.Now.ToString("MMddHHmmss"));

            File.Copy(fileName, destFile);
            XSSFWorkbook destWorkbook = null;

            using (FileStream destStream = new FileStream(destFile, FileMode.Open, FileAccess.Read))
            {
                destWorkbook = new XSSFWorkbook(destStream);
            }
            ISheet destSheet = destWorkbook.GetSheetAt(0);

            InsertColumn(lastValueColumnIndex, needAddedColumn, destSheet);

            IEnumerator rowEnumerator = destSheet.GetRowEnumerator();
            // 验证失败单元格格式
            ICellStyle abnormalStyle = CreateAbnormalStyle(destWorkbook);
            // 验证失败单元格同节点格式
            ICellStyle normalStyle = CreateNormalStyle(destWorkbook);

            if (rowEnumerator.MoveNext())
            {
                IRow destRow = destSheet.GetRow(0);
                for (int i = 0; i < needAddedColumn; i++)
                {
                    ICell cell = destRow.GetCell(lastValueColumnIndex + i + 1);
                    cell.SetCellValue(GetCellValue(destRow.GetCell(lastValueColumnIndex)));
                    CopyCell(destRow.GetCell(lastValueColumnIndex), cell, destWorkbook);
                }

                for (int columnIndex = destRow.FirstCellNum; columnIndex < destRow.LastCellNum - 1 && columnIndex <= lastValueColumnIndex + needAddedColumn; columnIndex++)
                {
                    ICell cell = destRow.GetCell(columnIndex);
                    columnHeader.Add(cell.ColumnIndex, cell == null ? "" : GetCellValue(cell));
                }
            }
            int    lastDirIndex = -1, appendColumn = lastValueColumnIndex, currentBufferIndex = 0;
            string key = "", value = "";

            string[]   buffer        = null;
            List <int> columnIndexes = new List <int>();

            while (rowEnumerator.MoveNext())
            {
                IRow        destRow        = rowEnumerator.Current as IRow;
                IEnumerator cellEnumerator = destRow.GetEnumerator();
                for (int columnIndex = destRow.FirstCellNum; columnIndex < destRow.LastCellNum - 1 && columnIndex <= lastValueColumnIndex + needAddedColumn; columnIndex++)
                {
                    ICell destCell = destRow.GetCell(columnIndex);
                    if (destCell == null)
                    {
                        continue;
                    }
                    switch (columnHeader[columnIndex] as string)
                    {
                    case "序号":
                        key = GetCellValue(destCell);
                        break;

                    case "参数项的值":
                        value = GetCellValue(destCell);
                        int  index   = 0;
                        bool flag    = false;
                        bool isTitle = false;
                        if (key != null && specialParameters.ContainsKey(key))
                        {
                            if (value.IndexOf(',') >= 0)
                            {
                                buffer = value.Split(',');
                            }
                            else if (value.IndexOf(',') >= 0)
                            {
                                buffer = value.Split(',');
                            }
                            else if (value.IndexOf(';') >= 0)
                            {
                                buffer = value.Split(';');
                            }
                            //if (buffer == null)
                            //    buffer = value.Split(',');
                            if (buffer != null && currentBufferIndex < buffer.Length)
                            {
                                columnIndexes.Add(destCell.ColumnIndex);
                                destCell.SetCellValue(buffer[currentBufferIndex]);
                                currentBufferIndex++;
                            }
                        }
                        flag = validator.IsValid(key, GetCellValue(destCell));
                        if (flag == false || validator.IsSpecial(key, GetCellValue(destCell)))
                        {
                            if (startIndex.Contains(destCell.ColumnIndex))
                            {
                                index   = (int)startIndex[destCell.ColumnIndex];
                                isTitle = false;
                            }
                            else
                            {
                                index   = lastDirIndex;
                                isTitle = true;
                            }
                            SetAbnormalCell(index, destCell.RowIndex, destCell.ColumnIndex, destSheet, abnormalStyle, normalStyle, isTitle);
                            startIndex[destCell.ColumnIndex] = destCell.RowIndex;
                        }
                        result = result && flag;
                        break;

                    case "参数项名称":
                        buffer = null;
                        string str   = GetCellValue(destCell);
                        Match  match = Default3CDataProvider.paraNameMatcher.Match(str);
                        if (match.Success && match.Groups["name"].Success)
                        {
                            str = match.Groups["name"].Value.Trim();
                        }
                        else
                        {
                            str = str.Trim();
                        }
                        if (treeDir.ContainsKey(str))
                        {
                            if (columnIndexes.Count > 1)
                            {
                                CopyCell(lastDirIndex, destCell.RowIndex, columnIndexes, destSheet);
                            }
                            columnIndexes.Clear();
                            lastDirIndex = destCell.RowIndex;
                            foreach (DictionaryEntry entry in startIndex)
                            {
                                SetAbnormalCell((int)entry.Value, lastDirIndex - 1, (int)entry.Key, destSheet, normalStyle, normalStyle, false);
                            }
                            startIndex.Clear();
                        }
                        currentBufferIndex = 0;
                        break;
                    }
                }
            }
            File.Delete(destFile);
            destFile = string.Format("{0}\\{3}_{1}_{2}.xlsx", Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName), DateTime.Now.ToString("MMddHHmmss"), result ? "已验证" : "验证失败");
            using (FileStream fileStream = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write))
            {
                destWorkbook.Write(fileStream);
            }
            return(result);
        }