-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSVWriter.cs
More file actions
62 lines (52 loc) · 1.43 KB
/
CSVWriter.cs
File metadata and controls
62 lines (52 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace Personal_Genome_Explorer
{
class CSVWriter : IDisposable
{
// The stream that the CSV is being written to.
private StreamWriter parentWriter;
// Whether the current row has any columns yet.
private bool bRowHasColumns = false;
public CSVWriter(StreamWriter inParentWriter)
{
Debug.Assert(inParentWriter != null);
parentWriter = inParentWriter;
}
public void AddColumn(string ColumnValue)
{
// If this isn't the first column in the row, add a comma to separate it from the previous column.
if(bRowHasColumns)
{
parentWriter.Write(',');
}
bRowHasColumns = true;
// Encode embedded double quotes as double double quotes.
ColumnValue = ColumnValue.Replace("\"","\"\"");
// Write the column string, delimited by double quotes.
parentWriter.Write("\"{0}\"",ColumnValue);
}
public void FlushRow()
{
// Write a newline to advance to the next row.
parentWriter.Write('\n');
// Indicate that the next column written is the first column in the row, and doesn't need to be preceded by a comma.
bRowHasColumns = false;
}
// IDisposable interface.
public void Dispose()
{
// Flush the last row to the file.
FlushRow();
if(parentWriter != null)
{
parentWriter.Dispose();
parentWriter = null;
}
}
}
}