Exemple #1
0
        /// <summary>
        /// Gets the range text.
        /// </summary>
        /// <param name="worksheet">The sheet.</param>
        /// <param name="row">The row index.</param>
        /// <param name="rowCount">The row count.</param>
        /// <param name="column">The column index.</param>
        /// <param name="columnCount">The column count.</param>
        /// <param name="rowDelimiter">The row delimiter.</param>
        /// <param name="columnDelimiter">The column delimiter.</param>
        /// <param name="cellDelimiter">The cell delimiter.</param>
        /// <param name="forceCellDelimiter">If set to <c>true</c>, [force cell delimiter]</param>
        /// <param name="flags">The export flags.</param>
        /// <returns></returns>
        public static string GetRangeText(Worksheet worksheet, int row, int rowCount, int column, int columnCount, string rowDelimiter, string columnDelimiter, string cellDelimiter, bool forceCellDelimiter, TextFileSaveFlags flags)
        {
            if (worksheet == null)
            {
                throw new ArgumentNullException("sheet");
            }
            if ((row < -1) || (row >= worksheet.RowCount))
            {
                throw new IndexOutOfRangeException(string.Format(ResourceStrings.InvaildRowIndexWithAllowedRangeBehind, (object[])new object[] { ((int)row), ((int)(worksheet.RowCount - 1)) }));
            }
            if ((rowCount < -1) || ((row + rowCount) > worksheet.RowCount))
            {
                throw new IndexOutOfRangeException(string.Format(ResourceStrings.InvalidRowCountWithAllowedRowCountBehind, (object[])new object[] { ((int)rowCount), ((int)(worksheet.RowCount - 1)) }));
            }
            if ((column < -1) || (column >= worksheet.ColumnCount))
            {
                throw new IndexOutOfRangeException(string.Format(ResourceStrings.InvalidColumnIndexWithAllowedRangeBehind, (object[])new object[] { ((int)column), ((int)(worksheet.ColumnCount - 1)) }));
            }
            if ((columnCount < -1) || ((column + columnCount) > worksheet.ColumnCount))
            {
                throw new IndexOutOfRangeException(string.Format(ResourceStrings.InvalidColumnCountWithAllowedColumnCountBehind, (object[])new object[] { ((int)columnCount), ((int)(worksheet.ColumnCount - columnCount)) }));
            }
            int rowEndIndex    = -1;
            int columnEndIndex = -1;

            if (((row == -1) && (column == -1)) && ((rowCount == -1) && (columnCount == -1)))
            {
                row            = 0;
                column         = 0;
                rowEndIndex    = worksheet.GetLastDirtyRow(StorageType.Data);
                columnEndIndex = worksheet.GetLastDirtyColumn(StorageType.Data);
            }
            else
            {
                if (row == -1)
                {
                    row = 0;
                }
                if (column == -1)
                {
                    column = 0;
                }
                if (rowCount == -1)
                {
                    rowCount = worksheet.RowCount - row;
                }
                if (columnCount == -1)
                {
                    columnCount = worksheet.ColumnCount - column;
                }
                rowEndIndex    = (row + rowCount) - 1;
                columnEndIndex = (column + columnCount) - 1;
            }
            if (string.IsNullOrEmpty(rowDelimiter))
            {
                rowDelimiter = "\r\n";
            }
            if (string.IsNullOrEmpty(columnDelimiter))
            {
                columnDelimiter = "\t";
            }
            if (string.IsNullOrEmpty(cellDelimiter))
            {
                cellDelimiter = "\"";
            }
            List <List <object> > list    = GetSheetData(worksheet, row, rowEndIndex, column, columnEndIndex, flags);
            StringBuilder         builder = new StringBuilder();

            for (int i = 0; i < list.Count; i++)
            {
                List <object> list2 = list[i];
                bool          flag  = true;
                foreach (object obj2 in list2)
                {
                    if (!flag)
                    {
                        builder.Append(columnDelimiter);
                    }
                    flag = false;
                    string str = string.Empty;
                    if (obj2 != null)
                    {
                        str = obj2.ToString().Replace(cellDelimiter, cellDelimiter + cellDelimiter);
                    }
                    if ((forceCellDelimiter || (str.IndexOf(cellDelimiter) != -1)) || ((str.IndexOf(columnDelimiter) != -1) || (str.IndexOf(rowDelimiter) != -1)))
                    {
                        builder.AppendFormat("{0}{1}{0}", (object[])new object[] { cellDelimiter, str });
                    }
                    else
                    {
                        builder.Append(str);
                    }
                }
                if (i != (list.Count - 1))
                {
                    builder.Append(rowDelimiter);
                }
            }
            return(builder.ToString());
        }