From a709667793521628e7da9f6e720c61a2f15bbfbd Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Wed, 21 Nov 2018 22:32:43 +0100 Subject: [PATCH] Aggiunta export su csv --- C-TRACK/WebUserControls/mod_periodo.ascx.cs | 1 + C-TRACK/doExport.aspx.cs | 63 ++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/C-TRACK/WebUserControls/mod_periodo.ascx.cs b/C-TRACK/WebUserControls/mod_periodo.ascx.cs index 6eece8f..b428c51 100644 --- a/C-TRACK/WebUserControls/mod_periodo.ascx.cs +++ b/C-TRACK/WebUserControls/mod_periodo.ascx.cs @@ -11,6 +11,7 @@ namespace C_TRACK.WebUserControls { dataFrom = DateTime.Today.AddMonths(-1); dataTo = DateTime.Today.AddDays(1); + updateUrl(); } } diff --git a/C-TRACK/doExport.aspx.cs b/C-TRACK/doExport.aspx.cs index f3e11ab..8be7ece 100644 --- a/C-TRACK/doExport.aspx.cs +++ b/C-TRACK/doExport.aspx.cs @@ -1,6 +1,9 @@ -using System; +using AppData; +using System; using System.Collections.Generic; +using System.Data; using System.Linq; +using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; @@ -11,6 +14,64 @@ namespace C_TRACK { protected void Page_Load(object sender, EventArgs e) { + string delimiter = ","; + + //prepare the output stream + Response.Clear(); + Response.ContentType = "text/csv"; + Response.AppendHeader("Content-Disposition", "attachment; filename=ctrack_report.csv"); + + string value = ""; + StringBuilder builder = new StringBuilder(); + + // recupero oggetti parametrici... + var dt = dataLayer.man.taTR.getByFilt("", DateTime.Today.AddMonths(-3), DateTime.Today.AddDays(1)); + + //write the csv column headers + for (int i = 0; i < dt.Columns.Count; i++) + { + + value = dt.Columns[i].ColumnName; + // Implement special handling for values that contain comma or quote + // Enclose in quotes and double up any double quotes + if (value.IndexOfAny(new char[] { '"', ',' }) != -1) + builder.AppendFormat("\"{0}\"", value.Replace("\"", "\"\"")); + else + { + builder.Append(value); + + } + + Response.Write(value); + Response.Write((i < dt.Columns.Count - 1) ? delimiter : Environment.NewLine); + builder.Clear(); + } + + //write the data + foreach (DataRow row in dt.Rows) + { + for (int i = 0; i < dt.Columns.Count; i++) + { + value = row[i].ToString(); + // Implement special handling for values that contain comma or quote + // Enclose in quotes and double up any double quotes + + if (value.IndexOfAny(new char[] { '"', ',' }) != -1) + builder.AppendFormat("\"{0}\"", value.Replace("\"", "\"\"")); + else + { + builder.Append(value); + + } + + Response.Write(builder.ToString()); + Response.Write((i < dt.Columns.Count - 1) ? delimiter : Environment.NewLine); + builder.Clear(); + } + } + + Response.End(); + } }