Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape a comma in data #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/ApplicationCore/ActionResults/CSVResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,29 @@ public class CSVResult<T> : IActionResult where T : class
{
private readonly IEnumerable<T> _data;

public CSVResult(IEnumerable<T> data, string fileName)
public CSVResult(IEnumerable<T> data, string fileName, bool spaceAfterComma)
{
_data = data;
FileName = fileName;
SpaceAfterComma = spaceAfterComma;
}

public string FileName { get; }
public bool SpaceAfterComma { get; }

public async Task ExecuteResultAsync(ActionContext context)
{
try
{
var csvBytes = await _data.GenerateCSVForDataAsync();
var csvBytes = await _data.GenerateCSVForDataAsync(SpaceAfterComma);
WriteExcelFileAsync(context.HttpContext, csvBytes);

}
catch (Exception e)
{
Console.WriteLine(e);

var errorBytes = await new List<T>().GenerateCSVForDataAsync();
var errorBytes = await new List<T>().GenerateCSVForDataAsync(SpaceAfterComma);
WriteExcelFileAsync(context.HttpContext, errorBytes);
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/ApplicationCore/Extensions/ReportExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,21 @@ public static async Task<byte[]> GenerateExcelForDataTableAsync<T>(this IEnumera
}
}

public static async Task<byte[]> GenerateCSVForDataAsync<T>(this IEnumerable<T> data)
public static async Task<byte[]> GenerateCSVForDataAsync<T>(this IEnumerable<T> data, bool spaceAfterComma)
{
var builder = new StringBuilder();
var stringWriter = new StringWriter(builder);

string comma = spaceAfterComma ? " " : string.Empty;

await Task.Run(() =>
{
var columns = GetColumnsFromModel(typeof(T)).ToDictionary(x => x.Name, x => x.Value).OrderBy(x => x.Value.Order);

foreach (var column in columns)
{
stringWriter.Write(column.Key);
stringWriter.Write(", ");
stringWriter.Write(","+ comma);
}
stringWriter.WriteLine();

Expand All @@ -134,7 +136,7 @@ await Task.Run(() =>
foreach (var prop in columns)
{
stringWriter.Write(PropertyExtensions.GetPropertyValue(item, prop.Value.Path));
stringWriter.Write(", ");
stringWriter.Write("," + comma);
}
stringWriter.WriteLine();
}
Expand Down