예제 #1
0
        /// <summary>
        /// Formats the data in the worksheet into a list of Strings.  Each field in the
        /// worksheet will be separated from the next by the specified delimiter, with
        /// no trailing delimiter. </summary>
        /// <param name="delimiter"> the character String to use to separate worksheet fields. </param>
        /// <param name="quotes"> if true, then the column names will be surrounded by
        /// quotes.  In addition, if the column data has the delimiter in it, it will be
        /// surrounded by quotes.  If false, nothing will be surrounded by quotes. </param>
        /// <returns> a list of delimited strings.  Each element in the list is one
        /// row in the worksheet. </returns>
        protected internal virtual IList <string> formatOutput(string delimiter, bool trimFieldValue, bool quotes)
        {
            IList <string> v = new List <string>();

            int rows = _worksheet.getRowCount();
            int cols = _worksheet.getColumnCount();

            StringBuilder buff  = new StringBuilder();
            string        quote = "\"";

            for (int i = 0; i < cols; i++)
            {
                if (quotes)
                {
                    buff.Append(quote);
                }
                buff.Append(_worksheet.getColumnName(i, true));
                if (quotes)
                {
                    buff.Append(quote);
                }
                if (i < cols - 1)
                {
                    buff.Append(delimiter);
                }
            }

            v.Add(buff.ToString());

            int    j = 0;
            string s = null;

            for (int i = 0; i < rows; i++)
            {
                buff.Length = 0;

                for (j = 0; j < cols; j++)
                {
                    s = _worksheet.getValueAtAsString(i, j);
                    if (trimFieldValue)
                    {
                        s = s.Trim();
                    }
                    if (s.IndexOf(delimiter, StringComparison.Ordinal) > -1 && quotes)
                    {
                        buff.Append("\"" + s + "\"");
                    }
                    else
                    {
                        buff.Append(s);
                    }

                    if (j < cols - 1)
                    {
                        // do not do for the last column.
                        buff.Append(delimiter);
                    }
                }

                v.Add(buff.ToString());
            }

            return(v);
        }