Beispiel #1
0
        /// <summary>
        /// Generates a CSV string based on the list of objects passed in.
        /// </summary>
        /// <param name="listToWriteOut">data to be written out</param>
        /// <param name="usePropertiesForColumns">True:  uses Properties for column definitions.  False:  uses Fields for column definitions.</param>
        /// <param name="fillInNULLs">if True, null values will be filled with NULL, false will be empty</param>
        /// <param name="IgnorePropertyList">Array of properties that are to be ignored</param>
        /// <returns>String representing the csv</returns>
        public static String GenerateCSVString(this IEnumerable listToWriteOut, IEnumerableExtensionOptions configOptions)
        {
            var firstLoop = true;
            var header    = new StringBuilder();
            var data      = new StringBuilder();

            // columns
            PropertyInfo[]? properties = null;
            FieldInfo[]? fields        = null;

            // loop through all objects
            foreach (var obj in listToWriteOut)
            {
                var dataRow = GetRowData(configOptions, firstLoop, header, ref properties, ref fields, obj);

                if (firstLoop && configOptions.ShowHeaderRow)
                {
                    firstLoop = false;
                    data.AppendLine(header.ToString().Trim(','));
                }
                // remove first ","
                data.AppendLine(dataRow.ToString().Remove(0, 1));
            }

            return(data.ToString());
        }
Beispiel #2
0
        /// <summary>
        /// Gets the data for the row.
        /// If first loop is true then also returns header information.
        /// </summary>
        /// <param name="configOptions"></param>
        /// <param name="firstLoop"></param>
        /// <param name="header"></param>
        /// <param name="properties"></param>
        /// <param name="fields"></param>
        /// <param name="obj"></param>
        /// <returns>string builder with row information.  If first row, then header stringbuilder is also filled in</returns>
        private static StringBuilder GetRowData(IEnumerableExtensionOptions configOptions, bool firstLoop, StringBuilder header, ref PropertyInfo[]?properties, ref FieldInfo[]?fields, object obj)
        {
            if (configOptions.UsePropertiesForColumns)
            {
                properties = obj.GetType().GetProperties().RemoveUnneededColumns(configOptions);
            }
            else
            {
                fields = obj.GetType().GetFields().RemoveUnneededColumns(configOptions);
            }

            var dataRow = new StringBuilder();

            // loop through all properties
            var fieldCountGreaterThan0 = (configOptions.UsePropertiesForColumns ? properties !.Length : fields !.Length) > 0;

            if (fieldCountGreaterThan0 && obj.GetType() != typeof(String))
            {
                for (int blah = 0; blah < (configOptions.UsePropertiesForColumns ? properties !.Length : fields !.Length); blah++)
                {
                    // on first loop generate the header
                    if (firstLoop)
                    {
                        if (configOptions.UsePropertiesForColumns)
                        {
                            header.Append(String.Format(",{0}", GetFieldName(properties ![blah].Name, configOptions)));
Beispiel #3
0
        /// <summary>
        /// Generates a CSV file based on the list of objects passed in.  File name is the class name.
        /// </summary>
        /// <param name="listToWriteOut">data to be written out</param>
        /// <param name="stream">Stream to be written to</param>
        /// <param name="usePropertiesForColumns">True:  uses Properties for column definitions.  False:  uses Fields for column definitions.</param>
        /// <param name="fillInNULLs">if True, null values will be filled with NULL, false will be empty</param>
        /// <param name="IgnoreList">properties/fields that are to be ignored</param>
        public async static Task GenerateCSVAsync(this IEnumerable listToWriteOut, Stream stream, IEnumerableExtensionOptions configOptions)
        {
            var sw        = new StreamWriter(stream);
            var firstLoop = true;
            var header    = new StringBuilder();

            // columns
            PropertyInfo[]? properties = null;
            FieldInfo[]? fields        = null;

            // loop through all objects
            foreach (var obj in listToWriteOut)
            {
                var dataRow = GetRowData(configOptions, firstLoop, header, ref properties, ref fields, obj);

                if (firstLoop && configOptions.ShowHeaderRow)
                {
                    firstLoop = false;
                    await sw.WriteLineAsync(header.ToString().Trim(','));
                }
                // remove first ","
                await sw.WriteLineAsync(dataRow.ToString().Remove(0, 1));
            }

            await sw.FlushAsync();
        }
Beispiel #4
0
        public static async Task GenerateCSVAsync(this IEnumerable <Dictionary <string, object> > listToWriteOut, Stream stream, IEnumerableExtensionOptions options)
        {
            var sw        = new StreamWriter(stream);
            var firstLoop = true;
            var header    = new StringBuilder();

            // loop through all objects
            foreach (var obj in listToWriteOut)
            {
                var dataRow = new StringBuilder();
                foreach (var item in obj)
                {
                    if (firstLoop && options.ShowHeaderRow)
                    {
                        header.Append($"{item.Key},");
                    }

                    var oValue = item.Value;
                    if (oValue == null && options.FillInNulls)
                    {
                        oValue = "Null";
                    }

                    if (oValue == null)
                    {
                        dataRow.Append(",");
                    }
                    else
                    {
                        Boolean typeIsString = item.Value.GetType() == typeof(String);
                        if (typeIsString)
                        {
                            dataRow.Append(String.Format(",\"{0}\"", oValue !.ToString().Replace("\"", "\"\"")));
                        }
                        else
                        {
                            dataRow.Append(String.Format(",{0}", oValue));
                        }
                    }
                }

                if (firstLoop && options.ShowHeaderRow)
                {
                    firstLoop = false;
                    await sw.WriteLineAsync(header.ToString().Trim(','));
                }
                // remove first ","
                await sw.WriteLineAsync(dataRow.ToString().Remove(0, 1));
            }

            await sw.FlushAsync();
        }