JamesFisher
JamesFisher

Reputation: 71

Export DataTable Contents to CSV Files

My code exports 3 csv files which is what I want to do. But my issue is that each csv file contains all data for all regions, as opposed to ONLY the data for the specified region.

What should I change so that I get 3 csv files containing only the data for the specified region?

private void GenerateRegionTest()
{
    DataTable regionInfo = new DataTable();
    regionInfo.Columns.Add("storeID", typeof(int));
    regionInfo.Columns.Add("storeName", typeof(string));
    regionInfo.Columns.Add("Region", typeof(string));
    regionInfo.Rows.Add(25, "Store 1", "Region 1");
    regionInfo.Rows.Add(50, "Store 2", "Region 1");
    regionInfo.Rows.Add(10, "Store 3", "Region 1");
    regionInfo.Rows.Add(21, "Store 4", "Region 1");
    regionInfo.Rows.Add(251, "Store 11", "Region 11");
    regionInfo.Rows.Add(501, "Store 21", "Region 11");
    regionInfo.Rows.Add(101, "Store 31", "Region 11");
    regionInfo.Rows.Add(211, "Store 41", "Region 11");
    regionInfo.Rows.Add(215, "Store 12", "Region 12");
    regionInfo.Rows.Add(510, "Store 22", "Region 12");
    regionInfo.Rows.Add(110, "Store 32", "Region 12");
    regionInfo.Rows.Add(211, "Store 42", "Region 12");
    StringBuilder sb = new StringBuilder();            
    var columnNames = dt.AsEnumerable().GroupBy((storeName) => storeName.Field<string>("Region"))
                                        .Select((group) => new
                                        {
                                            Region = group.Key,
                                            DataRowList = group.OrderBy((dataRow) => dataRow.Field<string>("storeName")).ToList()
                                        }).OrderBy(x => x.Region).ToList();

    sb.AppendLine(string.Join(",", columnNames));
    foreach (var row in columnNames)
    {
        foreach (var dataRow in row.DataRowList)
        {
            IEnumerable<string> fields = dataRow.ItemArray.Select(field => string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\""));
            sb.AppendLine(string.Join(",", fields));
        }
        string fileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) + "\\" + row + "Breakout.csv";
        File.WriteAllText(fileName, sb.ToString());
    }
}

Upvotes: 3

Views: 115

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460018

You have to move the initialization of the StringBuilder into the loop:

foreach (var row in columnNames)
{
    StringBuilder sb = new StringBuilder();
    sb.AppendLine(string.Join(",", columnNames));
    foreach (var dataRow in row.DataRowList)
    {
        IEnumerable<string> fields = dataRow.ItemArray.Select(field => string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\""));
        sb.AppendLine(string.Join(",", fields));
    }
    string fileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)) + "\\" + row + "Breakout.csv";
    File.WriteAllText(fileName, sb.ToString());
}

Upvotes: 1

Related Questions