PowerQueryNet icon indicating copy to clipboard operation
PowerQueryNet copied to clipboard

Commas not working as expected in CSV output

Open amks1 opened this issue 3 years ago • 1 comments

I've attached a screenshot of the returned CSV file to show the issue:

image

These are supposed to be a single column having URLs pointing to a geo coordinate. After looking through your code I understood the issue: when there are commas within quotes, it does not work and the entire string breaks.

 public static string ToDelimitedFile(this DataTable dataTable, char delimiter, bool inQuote)
        {
            StringBuilder content = new StringBuilder();

            string lastColumn = dataTable.Columns[dataTable.Columns.Count - 1].ColumnName;
            foreach (DataColumn dc in dataTable.Columns)
            {
                content.Append(dc.ColumnName);
                if (dc.ColumnName != lastColumn)
                    content.Append(delimiter);
            }

            int i = 0;
            foreach (DataRow dr in dataTable.Rows)
            {
                content.Append(Environment.NewLine);

                foreach (DataColumn dc in dataTable.Columns)
                {
                    if (inQuote)
                        content.Append('"' + dr[dc.ColumnName].ToString() + '"');
                    else
                        content.Append(dr[dc.ColumnName].ToString());

                    if (dc.ColumnName != lastColumn)
                        content.Append(delimiter);
                }
            }

            return content.ToString();
        }

amks1 avatar Jan 28 '21 10:01 amks1

You are right, this is a bug I will have to review.

gsimardnet avatar Jan 28 '21 22:01 gsimardnet