public void DoSaveCSV(object sender, EventArgs e) { CultureInfo csvCulture = CultureResolver.ResolveCsvCulture(Request); CSVFormatter csvformat = new CSVFormatter(csvCulture); // Create Header var filter = SearchFilter; bool isConfidentialityAffected = PollutantReleases.IsAffectedByConfidentiality(filter); Dictionary <string, string> header = EPRTR.HeaderBuilders.CsvHeaderBuilder.GetPollutantReleaseSearchHeader( filter, isConfidentialityAffected); // Create Body IEnumerable <PollutantReleases.ActivityTreeListRow> rows = PollutantReleases.GetActivityTree(filter); // dump to file string topheader = csvformat.CreateHeader(header); string[] colHeaderRows = csvformat.GetPollutantReleaseActivityColHeaderRows(filter); Response.WriteUtf8FileHeader("EPRTR_Pollutant_Releases_Activity_List"); Response.Write(topheader + colHeaderRows[0] + colHeaderRows[1]); //all rows but total foreach (var item in rows.Where(r => r.Code != ActivityTreeListRow.CODE_TOTAL)) { string row = csvformat.GetPollutantReleaseActivityRow(item, filter); Response.Write(row); } //write total row var totalRow = rows.SingleOrDefault(r => r.Code == ActivityTreeListRow.CODE_TOTAL); if (totalRow == null) { //find all rows on topLevel. if only one, use this as total row var toplevelRows = rows.Where(r => r.Level == 0); if (toplevelRows != null && toplevelRows.Count() == 1) { totalRow = toplevelRows.Single(); } } //write total row if any is found if (totalRow != null) { Response.Write(Environment.NewLine); Response.Write(csvformat.AddText(Resources.GetGlobal("Common", "Total"))); Response.Write(Environment.NewLine); Response.Write(colHeaderRows[0] + colHeaderRows[1]); string row = csvformat.GetPollutantReleaseActivityRow(totalRow, filter); Response.Write(row); } Response.End(); }