Hey guys. I have a program which takes a pipe delimited query, fills a data table with the results and then the data table is passed to a class that produces a CSV file. My problem is in producing a CSV file that is appropriately "pipe-delimited", that Excel will recognize and give me the option to delimit my file accordingly.
A snippet of my pipe-delimited query looks thusly:
+ '|' + CAST(ISNULL(ls.LoanOfficerName, '') AS VARCHAR)
+ '|' + CAST(ISNULL(ls.BorrowerLastName, '') AS VARCHAR)
As you can see, it is delimited not by comma but by a pipe (+ '|' +).
The code that my dataTable is being passed into to geneate the CSV File is this:
public void WriteCSVFile(DataTable dataTable, string filePath)
{
StreamWriter sw = new StreamWriter(filePath, false);
int iColCount = dataTable.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dataTable.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
foreach (DataRow row in dataTable.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(row[i]))
{
sw.Write(row[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
Incidentally, this code does not allow me to open the CSV file and designate it as a pipe delimited file. What is it about my above code that I can change to permit a pipe delimited output?