Beispiel #1
0
        private bool DoColumnSingleMerged(MergedConfig config)
        {
            bool mergedResult = true;
            int  fromColumn   = -1;
            int  toClumn      = -1;

            if (!int.TryParse(config.FromColumn, out fromColumn))
            {
                throw new ArgumentException("Merge配置中fromColumn参数不能为空");
            }
            if (!int.TryParse(config.ToColumn, out toClumn))
            {
                throw new ArgumentException("Merge配置中ToColumn参数不能为空或者只能为数字");
            }
            if (fromColumn > toClumn)
            {
                throw new ArgumentException("Merge配置中fromColumn参数不能大于ToColumn");
            }

            for (int i = fromColumn; i <= toClumn; i++)
            {
                this.DoColumnSingleMerged(config, i);
            }

            return(mergedResult);
        }
Beispiel #2
0
        private void RecursivelyMerged(int fromRow, int fromColumn, int toRow, int toColumn, MergedConfig orgianConfig)
        {
            bool isIndentity    = false;
            int  endMergedIndex = fromColumn;

            for (int loopIndex = toColumn; loopIndex >= fromColumn; loopIndex--)
            {
                isIndentity = ValidIdentity(fromRow, fromColumn, toRow, loopIndex);
                if (isIndentity)
                {
                    endMergedIndex = loopIndex;
                    break;
                }
            }

            CurrentSheet.AddMergedRegion(new CellRangeAddress(fromRow, fromColumn, toRow, endMergedIndex));

            if (toColumn - endMergedIndex > 0)
            {
                MergedConfig config = new MergedConfig();
                config.MergedType         = orgianConfig.MergedType;
                config.MergedTypePriority = orgianConfig.MergedTypePriority;
                config.IgnoreEmpty        = orgianConfig.IgnoreEmpty;
                config.NeedValid          = orgianConfig.NeedValid;
                config.FromRow            = fromRow + "";
                config.FromColumn         = (endMergedIndex + 1).ToString();
                config.ToRow    = toRow + "";
                config.ToColumn = toColumn + "";
                DoAllSingleMerged(config);
            }
        }
Beispiel #3
0
        private bool DoSingleMerged(MergedConfig config)
        {
            bool mergedResult = true;

            if (config.MergedType == MergedConfig.RowMergedType)
            {
                mergedResult = DoRowSingleMerged(config);
            }
            else if (config.MergedType == MergedConfig.ColumnMergedType)
            {
                mergedResult = DoColumnSingleMerged(config);
            }
            else if (config.MergedType == MergedConfig.AllMergedType)
            {
                mergedResult = DoAllSingleMerged(config);
            }


            return(mergedResult);
        }
Beispiel #4
0
        private void DoColumnSingleMerged(MergedConfig config, int indexColumn)
        {
            int fromRow = -1;

            if (!int.TryParse(config.FromRow, out fromRow))
            {
                fromRow = StartRow;
            }
            int loopIndex = fromRow;

            while (loopIndex <= EndRow)
            {
                int    startRowIndex     = loopIndex;
                IRow   hssRow            = CurrentSheet.GetRow(loopIndex);
                string standCurrentValue = GetCellValue(hssRow.GetCell(indexColumn));
                for (++loopIndex; loopIndex <= EndRow; loopIndex++)
                {
                    IRow   loopHssRow = CurrentSheet.GetRow(loopIndex);
                    string loopValue  = GetCellValue(loopHssRow.GetCell(indexColumn));

                    if (string.Compare(standCurrentValue, loopValue) != 0)
                    {
                        if (loopIndex - fromRow > 2)
                        {
                            CurrentSheet.AddMergedRegion(new CellRangeAddress(startRowIndex, indexColumn, loopIndex - 1, indexColumn));
                        }
                        break;
                    }

                    else if (loopIndex == EndRow)
                    {
                        CurrentSheet.AddMergedRegion(new CellRangeAddress(startRowIndex, indexColumn, loopIndex, indexColumn));
                    }
                }
            }
        }
Beispiel #5
0
        private MergedConfig PareseSigleConfig(XmlNode xNode)
        {
            MergedConfig config     = new MergedConfig();
            string       mergedType = XmlUtility.getNodeAttributeStringValue(xNode, "type");

            if (string.IsNullOrEmpty(mergedType))
            {
                throw new ArgumentException("Merged配置项中type不能为空");
            }
            if (string.Compare(mergedType, RowMergedType, true) == 0)
            {
                config.MergedType = MergedConfig.RowMergedType;
            }
            else if (string.Compare(mergedType, ColumnMergedType, true) == 0)
            {
                config.MergedType = MergedConfig.ColumnMergedType;
            }
            else if (string.Compare(mergedType, AllMergedType, true) == 0)
            {
                config.MergedType = MergedConfig.AllMergedType;
            }
            else
            {
                throw new ArgumentException("Merged配置项中type无效");
            }

            string areaID = XmlUtility.getNodeAttributeStringValue(xNode, "AreaID");

            if (string.IsNullOrEmpty(areaID))
            {
                throw new ArgumentException("Merged配置项中AreaID无效");
            }
            config.AreaId     = areaID;
            config.FromColumn = XmlUtility.getNodeAttributeStringValue(xNode, "FromColumn");
            config.FromRow    = XmlUtility.getNodeAttributeStringValue(xNode, "FromRow");
            config.ToColumn   = XmlUtility.getNodeAttributeStringValue(xNode, "ToColumn");
            config.ToRow      = XmlUtility.getNodeAttributeStringValue(xNode, "ToRow");
            string mergedTypePriority = XmlUtility.getNodeAttributeStringValue(xNode, "MergedTypePriority");

            if (string.Compare(mergedTypePriority, ColumnMergedTypePriority, true) == 0)
            {
                config.MergedTypePriority = MergedConfig.ColumnMergedTypePriority;
            }
            else
            {
                config.MergedTypePriority = MergedConfig.RowMergedType;
            }
            string needValid = XmlUtility.getNodeAttributeStringValue(xNode, "NeedValid");

            if (string.Compare(needValid, "false", true) == 0)
            {
                config.NeedValid = false;
            }
            else
            {
                config.NeedValid = true;
            }

            string ignoreEmpty = XmlUtility.getNodeAttributeStringValue(xNode, "IgnoreEmpty");

            if (string.Compare(ignoreEmpty, "false", true) == 0)
            {
                config.IgnoreEmpty = false;
            }
            else
            {
                config.IgnoreEmpty = true;
            }
            return(config);
        }
Beispiel #6
0
        private bool DoRowSingleMerged(MergedConfig config)
        {
            bool mergedResult = true;
            int  fromColumn   = -1;
            int  toClumn      = -1;

            if (!int.TryParse(config.FromColumn, out fromColumn))
            {
                throw new ArgumentException("Merged配置项中fromColumn参数无效");
            }

            if (!int.TryParse(config.ToColumn, out toClumn))
            {
                throw new ArgumentException("Merged配置项中toColumn参数无效");
            }
            if (fromColumn > toClumn)
            {
                throw new ArgumentException("Merged配置中from参数不能大于toColumn参数");
            }
            if (fromColumn == toClumn)
            {
                return(true);
            }

            for (int index = StartRow; index <= EndRow; index++)
            {
                IRow   hssRow       = CurrentSheet.GetRow(index);
                bool   identityFlag = true;
                string lastRowValue = string.Empty;

                for (int columnIndex = fromColumn; columnIndex <= toClumn; columnIndex++)
                {
                    string currentRowValue = string.Empty;
                    ICell  currentCell     = hssRow.GetCell(columnIndex);
                    currentRowValue = GetCellValue(currentCell);

                    if (config.IgnoreEmpty)
                    {
                        if (string.IsNullOrEmpty(currentRowValue))
                        {
                            identityFlag = false;
                            break;
                        }
                    }

                    if (columnIndex > fromColumn)
                    {
                        identityFlag = string.Compare(currentRowValue, lastRowValue) == 0;
                        if (!identityFlag)
                        {
                            break;
                        }
                    }
                    lastRowValue = currentRowValue;
                }

                if (identityFlag)
                {
                    CurrentSheet.AddMergedRegion(new CellRangeAddress(index, fromColumn, index, toClumn));
                }
            }
            return(mergedResult);
        }
Beispiel #7
0
        private bool DoAllSingleMerged(MergedConfig config)
        {
            bool mergedResult = true;
            int  fromColumn   = -1;
            int  fromRow      = -1;
            int  toClumn      = -1;
            int  toRow        = -1;

            if (!int.TryParse(config.FromColumn, out fromColumn))
            {
                throw new ArgumentException("Merge配置中fromColumn参数不能为空或者只能为数字");
            }
            if (!int.TryParse(config.ToColumn, out toClumn))
            {
                throw new ArgumentException("Merge配置中ToColumn参数不能为空或者只能为数字");
            }
            if (fromColumn > toClumn)
            {
                throw new ArgumentException("Merge配置中fromColumn参数不能大于ToColumn");
            }

            if (!int.TryParse(config.FromRow, out fromRow))
            {
                fromRow = StartRow;
            }


            if (!int.TryParse(config.ToRow, out toRow))
            {
                toRow = EndRow;
            }
            int loopIndex = fromRow;

            while (loopIndex <= toRow)
            {
                int    startRowIndex     = loopIndex;
                IRow   hssRow            = CurrentSheet.GetRow(loopIndex);
                string standCurrentValue = GetCellValue(hssRow.GetCell(fromColumn));

                for (++loopIndex; loopIndex <= toRow; loopIndex++)
                {
                    IRow   loopHssRow = CurrentSheet.GetRow(loopIndex);
                    string loopValue  = GetCellValue(loopHssRow.GetCell(fromColumn));

                    if (string.Compare(standCurrentValue, loopValue) != 0)
                    {
                        if (loopIndex - startRowIndex > 2)
                        {
                            RecursivelyMerged(startRowIndex, fromColumn, loopIndex - 1, toClumn, config);
                        }
                        break;
                    }
                    else if (loopIndex == toRow)
                    {
                        RecursivelyMerged(startRowIndex, fromColumn, loopIndex, toClumn, config);
                    }
                }
            }

            return(mergedResult);
        }