Estoy intentando exportar una lista a un archivo CSV. Lo tengo todo funcionando hasta el punto en que quiero escribir en el archivo del flujo de respuesta. Esto no hace nada.
Aquí está mi código:
Llame al método de la página.
$('#btn_export').click(function () {
$.post('NewsLetter/Export');
});
El código en el controlador es el siguiente:
[HttpPost]
public void Export()
{
try
{
var filter = Session[FilterSessionKey] != null ? Session[FilterSessionKey] as SubscriberFilter : new SubscriberFilter();
var predicate = _subscriberService.BuildPredicate(filter);
var compiledPredicate = predicate.Compile();
var filterRecords = _subscriberService.GetSubscribersInGroup().Where(x => !x.IsDeleted).AsEnumerable().Where(compiledPredicate).GroupBy(s => s.Subscriber.EmailAddress).OrderBy(x => x.Key);
ExportAsCSV(filterRecords);
}
catch (Exception exception)
{
Logger.WriteLog(LogLevel.Error, exception);
}
}
private void ExportAsCSV(IEnumerable<IGrouping<String, SubscriberInGroup>> filterRecords)
{
var sw = new StringWriter();
//write the header
sw.WriteLine(String.Format("{0},{1},{2},{3}", CMSMessages.EmailAddress, CMSMessages.Gender, CMSMessages.FirstName, CMSMessages.LastName));
//write every subscriber to the file
var resourceManager = new ResourceManager(typeof(CMSMessages));
foreach (var record in filterRecords.Select(x => x.First().Subscriber))
{
sw.WriteLine(String.Format("{0},{1},{2},{3}", record.EmailAddress, record.Gender.HasValue ? resourceManager.GetString(record.Gender.ToString()) : "", record.FirstName, record.LastName));
}
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=adressenbestand.csv");
Response.ContentType = "text/csv";
Response.Write(sw);
Response.End();
}
Pero después de que Response.Write(sw)no pasa nada. ¿Es posible guardar un archivo de esta manera?
Saludos
Editar
Los encabezados de respuesta que veo cuando hago clic en el botón son:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/csv; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 2.0
Content-Disposition: attachment; filename=adressenbestand.csv
X-Powered-By: ASP.NET
Date: Wed, 12 Jan 2011 13:05:42 GMT
Content-Length: 113
Lo que me parece bien ...
Editar
Me deshice de la parte de jQuery y la reemplacé por un hipervínculo y esto funciona bien para mí ahora:
<a class="export" href="NewsLetter/Export">exporteren</a>
c#
jquery
asp.net-mvc
csv
Gerard
fuente
fuente

Respuestas:
yan.kun estaba en el camino correcto, pero esto es mucho más fácil.
public FileContentResult DownloadCSV() { string csv = "Charlie, Chaplin, Chuckles"; return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv", "Report123.csv"); }fuente
<a href="ControllerName/ActionName">Download CSV</a>Con MVC, simplemente puede devolver un archivo como este:
public ActionResult ExportData() { System.IO.FileInfo exportFile = //create your ExportFile return File(exportFile.FullName, "text/csv", string.Format("Export-{0}.csv", DateTime.Now.ToString("yyyyMMdd-HHmmss"))); }fuente
Además de la respuesta de Biff MaGriff. Para exportar el archivo usando JQuery, redirija al usuario a una nueva página.
$('#btn_export').click(function () { window.location.href = 'NewsLetter/Export'; });fuente
¿Qué pasa si te deshaces del guionista?
Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=adressenbestand.csv"); Response.ContentType = "text/csv"; //write the header Response.Write(String.Format("{0},{1},{2},{3}", CMSMessages.EmailAddress, CMSMessages.Gender, CMSMessages.FirstName, CMSMessages.LastName)); //write every subscriber to the file var resourceManager = new ResourceManager(typeof(CMSMessages)); foreach (var record in filterRecords.Select(x => x.First().Subscriber)) { Response.Write(String.Format("{0},{1},{2},{3}", record.EmailAddress, record.Gender.HasValue ? resourceManager.GetString(record.Gender.ToString()) : "", record.FirstName, record.LastName)); } Response.End();fuente
Con respecto a Biff, aquí hay algunos ajustes que me permiten usar el método para rebotar CSV de jQuery / Post en el servidor y regresar como un mensaje CSV al usuario.
[Themed(false)] public FileContentResult DownloadCSV() { var csvStringData = new StreamReader(Request.InputStream).ReadToEnd(); csvStringData = Uri.UnescapeDataString(csvStringData.Replace("mydata=", "")); return File(new System.Text.UTF8Encoding().GetBytes(csvStringData), "text/csv", "report.csv"); }Necesitará la línea unescape si está presionando esto desde un formulario con un código como el siguiente,
var input = $("<input>").attr("type", "hidden").attr("name", "mydata").val(data); $('#downloadForm').append($(input)); $("#downloadForm").submit();fuente
Desde un botón en la vista, llame a .click (llame a algún script java). Desde allí, llame al método del controlador mediante window.location.href = 'Controlador / Método';
En el controlador, llame a la base de datos y obtenga la tabla de datos o llame a algún método, obtenga los datos de la tabla de la base de datos a una tabla de datos y luego haga lo siguiente,
using (DataTable dt = new DataTable()) { sda.Fill(dt); //Build the CSV file data as a Comma separated string. string csv = string.Empty; foreach (DataColumn column in dt.Columns) { //Add the Header row for CSV file. csv += column.ColumnName + ','; } //Add new line. csv += "\r\n"; foreach (DataRow row in dt.Rows) { foreach (DataColumn column in dt.Columns) { //Add the Data rows. csv += row[column.ColumnName].ToString().Replace(",", ";") + ','; } //Add new line. csv += "\r\n"; } //Download the CSV file. Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=SqlExport"+DateTime.Now+".csv"); Response.Charset = ""; //Response.ContentType = "application/text"; Response.ContentType = "application/x-msexcel"; Response.Output.Write(csv); Response.Flush(); Response.End(); }fuente
Incluso si ha resuelto su problema, aquí hay otro intento de exportar csv usando mvc.
return new FileStreamResult(fileStream, "text/csv") { FileDownloadName = fileDownloadName };fuente
Creo que te has olvidado de usar
debajo
por favor, compruebe
fuente
Crear archivo de Excel simple en MVC 4
public ActionResult results () {return File (nuevo System.Text.UTF8Encoding (). GetBytes ("cadena de datos"), "application / csv", "filename.csv"); }
fuente