Example #1
0
        /// <summary>
        /// To sort data within the table. Note that the sorting is only done when the table is inserted into the worksheet.
        /// </summary>
        /// <param name="TableColumnIndex">The table column index. For example, 1 for the 1st table column, 2 for the 2nd table column and so on.</param>
        /// <param name="SortAscending">True to sort in ascending order. False to sort in descending order.</param>
        public void Sort(int TableColumnIndex, bool SortAscending)
        {
            --TableColumnIndex;
            if (TableColumnIndex < 0 || TableColumnIndex >= this.TableColumns.Count)
            {
                return;
            }

            int iStartRowIndex = -1;
            int iEndRowIndex   = -1;

            if (this.HeaderRowCount > 0)
            {
                iStartRowIndex = this.StartRowIndex + 1;
            }
            else
            {
                iStartRowIndex = this.StartRowIndex;
            }
            // not inclusive of the last totals row
            if (this.TotalsRowCount > 0)
            {
                iEndRowIndex = this.EndRowIndex - 1;
            }
            else
            {
                iEndRowIndex = this.EndRowIndex;
            }

            this.SortState = new SLSortState();
            this.SortState.StartRowIndex    = iStartRowIndex;
            this.SortState.EndRowIndex      = iEndRowIndex;
            this.SortState.StartColumnIndex = this.StartColumnIndex;
            this.SortState.EndColumnIndex   = this.EndColumnIndex;

            SLSortCondition sc = new SLSortCondition();

            sc.StartRowIndex    = iStartRowIndex;
            sc.StartColumnIndex = this.StartColumnIndex + TableColumnIndex;
            sc.EndRowIndex      = iEndRowIndex;
            sc.EndColumnIndex   = sc.StartColumnIndex;
            if (!SortAscending)
            {
                sc.Descending = true;
            }
            this.SortState.SortConditions.Add(sc);

            this.HasSortState = true;
        }
        internal SLSortCondition Clone()
        {
            SLSortCondition sc = new SLSortCondition();

            sc.Descending       = this.Descending;
            sc.HasSortBy        = this.HasSortBy;
            sc.vSortBy          = this.vSortBy;
            sc.StartRowIndex    = this.StartRowIndex;
            sc.StartColumnIndex = this.StartColumnIndex;
            sc.EndRowIndex      = this.EndRowIndex;
            sc.EndColumnIndex   = this.EndColumnIndex;
            sc.CustomList       = this.CustomList;
            sc.FormatId         = this.FormatId;
            sc.HasIconSet       = this.HasIconSet;
            sc.vIconSet         = this.vIconSet;
            sc.IconId           = this.IconId;

            return(sc);
        }
        internal void FromSortState(SortState ss)
        {
            this.SetAllNull();

            if (ss.ColumnSort != null && ss.ColumnSort.Value)
            {
                this.ColumnSort = ss.ColumnSort.Value;
            }
            if (ss.CaseSensitive != null && ss.CaseSensitive.Value)
            {
                this.CaseSensitive = ss.CaseSensitive.Value;
            }
            if (ss.SortMethod != null)
            {
                this.SortMethod = ss.SortMethod.Value;
            }

            int    iStartRowIndex    = 1;
            int    iStartColumnIndex = 1;
            int    iEndRowIndex      = 1;
            int    iEndColumnIndex   = 1;
            string sRef = ss.Reference.Value;

            if (sRef.IndexOf(":") > 0)
            {
                if (SLTool.FormatCellReferenceRangeToRowColumnIndex(sRef, out iStartRowIndex, out iStartColumnIndex, out iEndRowIndex, out iEndColumnIndex))
                {
                    this.StartRowIndex    = iStartRowIndex;
                    this.StartColumnIndex = iStartColumnIndex;
                    this.EndRowIndex      = iEndRowIndex;
                    this.EndColumnIndex   = iEndColumnIndex;
                }
            }
            else
            {
                if (SLTool.FormatCellReferenceToRowColumnIndex(sRef, out iStartRowIndex, out iStartColumnIndex))
                {
                    this.StartRowIndex    = iStartRowIndex;
                    this.StartColumnIndex = iStartColumnIndex;
                    this.EndRowIndex      = iStartRowIndex;
                    this.EndColumnIndex   = iStartColumnIndex;
                }
            }

            if (ss.HasChildren)
            {
                SLSortCondition sc;
                using (OpenXmlReader oxr = OpenXmlReader.Create(ss))
                {
                    while (oxr.Read())
                    {
                        if (oxr.ElementType == typeof(SortCondition))
                        {
                            sc = new SLSortCondition();
                            sc.FromSortCondition((SortCondition)oxr.LoadCurrentElement());
                            // limit of 64 from Open XML specs
                            if (this.SortConditions.Count < 64)
                            {
                                this.SortConditions.Add(sc);
                            }
                        }
                    }
                }
            }
        }