I have a MVC controller that should return a CSV file from a string[][]
. I'm trying this example:
public FileContentResult DownloadCSV()
{
string[][] output = new string[][]{
new string[]{"Col 1 Row 1", "Col 2 Row 1", "Col 3 Row 1"},
new string[]{"Col1 Row 2", "Col2 Row 2", "Col3 Row 2"}
};
return File(output, "text/csv", "Report123.csv");
}
But I don't know how to create the file. How can I create a CSV file from string[][]
?
Here is a sample using LINQ.
public FileContentResult DownloadCSV()
{
string[][] output = new string[][]{
new string[]{"Col 1 Row 1", "Col 2 Row 1", "Col 3 Row 1"},
new string[]{"Col1 Row 2", "Col2 Row 2", "Col3 Row 2"}
};
var result = lines.Select(l=>string.Join(",", l))
.Aggregate(new StringBuilder(), (sb, v) => sb.AppendLine(v))
.ToString();
return File(result, "text/csv", "Report123.csv");
}
... as an extension method ...
public static class ToolsEx
{
public static string ToCsvString(this string[][] lines)
{
var query = lines.Select(l=>string.Join(",", l));
var result = query.Aggregate(new StringBuilder(), (sb, v) => sb.AppendLine(v));
return result.ToString();
}
}
... after remembering that string.Join
uses StringBuilder internal you could reduce this further to just be (this might be a bit much if someone else is going to maintain this after you) ...
public static string ToCsvString(this string[][] rows)
{
return string.Join(Environment.NewLine, rows.Select(row => string.Join(",", row)));
}
Use an extension method to generate the csv file
/// <summary>
/// Convert an array to string list, of the form "1,2,3,.."
/// </summary>
/// <param name="array">The array of numbers</param>
/// <returns>A string value</returns>
public static string ToCSVRow<T>(this T[] array)
{
string[] parts=new string[array.Length];
for(int i=0; i<parts.Length; i++)
{
parts[i]=array[i].ToString();
}
return string.Join(",", parts);
}
/// <summary>
/// Convert a jagged array to csv table, where each row has the form "1,2,3,.."
/// </summary>
/// <param name="array">The array of numbers</param>
/// <returns>A string value</returns>
public static string ToCSV<T>(this T[][] array)
{
List<string> csv=new List<string>();
for(int i=0; i<array.Length; i++)
{
csv.Add(array[i].ToCSVRow());
}
return string.Join(Environment.NewLine, csv.ToArray());
}
and use with
return File(output.ToCSV(), "text/csv", "Report123.csv");
Edit 1
Based on the comments it would be simpler and maybe faster to do the following:
/// <summary>
/// Convert an array to a csv row, of the form "1,2,3,4.."
/// </summary>
/// <typeparam name="T">The array type</typeparam>
/// <param name="list">The array</param>
/// <returns>A comma delimited string</returns>
public static string ToCSVRow<T>(this T[] list)
{
return string.Join(",", list);
}
/// <summary>
/// Convert a jagged array to csv table, where each row has the form "1,2,3,.."
/// </summary>
/// <param name="array">The array of numbers</param>
/// <returns>A string value</returns>
public static string ToCSV<T>(this T[][] array)
{
return string.Join(Environment.NewLine, array.Select((row) => string.Join(",", row)));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.