public void Carica(string nome, string cognome) { _Dt1 = PazienteDB.PazientiList(nome, cognome); Session[ToString()] = _Dt1; view = _Dt1.DefaultView; _InitViewParams(); }
protected void Export_Pazienti(object sender, EventArgs e) { var pazienti = PazienteDB.PazientiList(); //var pathParts = new List<string>(Server.MapPath("~/").Split(Path.DirectorySeparatorChar)); //pathParts.RemoveAt(pathParts.Count - 1); //pathParts.RemoveAt(pathParts.Count - 1); //pathParts.Add("exports"); //pathParts.Add("export_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm") + ".csv"); var fileName = "export_" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm") + ".csv"; byte[] buffer; //var filePath = String.Join(Path.DirectorySeparatorChar.ToString(), pathParts.ToArray()); //using (StreamWriter textWriter = File.CreateText(filePath)) using (var myMemoryStream = new MemoryStream()) using (StreamWriter textWriter = new StreamWriter(myMemoryStream, Encoding.ASCII)) { var csv = new CsvWriter(textWriter); var columnNames = new[] { "ID", "nome", "cognome", "email", "telefono", "cellulare" }; foreach (var columnName in columnNames) { csv.WriteField(columnName); } csv.NextRecord(); foreach (DataRow row in pazienti.Rows) { foreach (var columnName in columnNames) { var val = row[columnName].ToString().Trim(); if (columnName == "cellulare") { val = ConvertToCellNumberOrEmpty(val, row["telefono"].ToString().Trim()); } csv.WriteField(val); } csv.NextRecord(); } textWriter.Flush(); buffer = myMemoryStream.ToArray(); } Response.Clear(); Response.ContentType = "text/csv"; Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); Response.BinaryWrite(buffer); // myMemoryStream.WriteTo(Response.OutputStream); //works too Response.Flush(); Response.Close(); Response.End(); }