Created
May 24, 2013 20:09
-
-
Save jbubriski/5646178 to your computer and use it in GitHub Desktop.
A CSV Writer that can be used to write directly to a response stream in ASP.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
namespace DataUtils | |
{ | |
/// <summary> | |
/// Derived from Richard Carr's (Black Wasp) CsvWriter class at http://blackwasp.co.uk/WriteCsv.aspx | |
/// </summary> | |
public class CsvWriter : StreamWriter | |
{ | |
char _delimiter, _quote; | |
string _singleQuote, _doubleQuote; | |
List<char> _quotableCharacters; | |
string _commentToken; | |
public char Delimiter | |
{ | |
get | |
{ | |
return _delimiter; | |
} | |
set | |
{ | |
_delimiter = value; | |
ResetQuotableCharacters(); | |
} | |
} | |
public char Quote | |
{ | |
get | |
{ | |
return _quote; | |
} | |
set | |
{ | |
_quote = value; | |
_singleQuote = value.ToString(); | |
_doubleQuote = _singleQuote + _singleQuote; | |
ResetQuotableCharacters(); | |
} | |
} | |
public string CommentToken | |
{ | |
get | |
{ | |
return _commentToken; | |
} | |
set | |
{ | |
if (value == null) throw new ArgumentNullException("value"); | |
if (value.Length == 0) throw new ArgumentException("Value must not be empty"); | |
_commentToken = value; | |
} | |
} | |
private void ResetQuotableCharacters() | |
{ | |
_quotableCharacters = new List<char>(); | |
_quotableCharacters.AddRange(Environment.NewLine.ToCharArray()); | |
_quotableCharacters.Add(_delimiter); | |
_quotableCharacters.Add(_quote); | |
} | |
public CsvWriter(Stream stream) | |
: base(stream) | |
{ | |
Delimiter = ','; | |
Quote = '"'; | |
CommentToken = "## "; | |
} | |
public void WriteLine(string[] fields) | |
{ | |
bool first = true; | |
foreach (string field in fields) | |
{ | |
WriteField(first, field); | |
first = false; | |
} | |
WriteLine(); | |
} | |
private void WriteField(bool first, string field) | |
{ | |
if (!first) | |
{ | |
Write(_delimiter); | |
} | |
WritePreparedField(field); | |
} | |
private void WritePreparedField(string field) | |
{ | |
string prepared = PrepareField(field); | |
Write(prepared); | |
} | |
private string PrepareField(string field) | |
{ | |
string quotesDoubled = DoubleQuotes(field); | |
string prepared = AddQuotesIfRequired(quotesDoubled); | |
return prepared; | |
} | |
private string DoubleQuotes(string field) | |
{ | |
return field.Replace(_singleQuote, _doubleQuote); | |
} | |
private string AddQuotesIfRequired(string field) | |
{ | |
foreach (char quotable in _quotableCharacters) | |
{ | |
if (field.StartsWith(_commentToken) || field.Contains(quotable)) | |
{ | |
return string.Format("{0}{1}{0}", _quote, field); | |
} | |
} | |
return field; | |
} | |
public void WriteComment(string comment) | |
{ | |
Write(_commentToken); | |
WriteLine(comment); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
using DataUtils; | |
namespace WebApplication1 | |
{ | |
public partial class Default : System.Web.UI.Page | |
{ | |
protected void Unnamed1_Click(object sender, EventArgs e) | |
{ | |
Response.Clear(); | |
Response.ContentType = "text/csv"; | |
Response.AddHeader("Content-Disposition", "attachment; filename=data-export.csv"); | |
var csvWriter = new CsvWriter(Response.OutputStream); | |
// Output column list | |
var columns = new List<string>(); | |
columns.Add("Column 1"); | |
columns.Add("Column 2"); | |
csvWriter.WriteLine(columns.ToArray()); | |
// Output data | |
// In your loop, write the columns | |
//for (int i = 0; i < UPPER; i++) | |
//{ | |
var data = new List<string>(); | |
data.Add("data 1"); | |
data.Add("data 2"); | |
csvWriter.WriteLine(data.ToArray()); | |
//} | |
csvWriter.Flush(); | |
Response.End(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the option for "Response.OutputStream" in ASP.NET core