简体   繁体   English

与出口清单有关的问题 <T> 在MVC5中转换为CSV文件

[英]Issue Related to Export List<T> to CSV file In MVC5

I have webapplication in MVC5.. i have to export some data to .csv file for that i searched for article and found following code on bitbucket.. 我在MVC5中有Web应用程序。我必须将一些数据导出到.csv文件,以进行搜索文章并在bitbucket上找到以下代码。

 public class CsvActionResult<T> : FileResult
    {
        private readonly IList<T> _list;
        private readonly char _separator;

        public CsvActionResult(IList<T> list,
        string fileDownloadName,
        char separator = ',')
            : base("text/csv")
        {
            _list = list;
            FileDownloadName = fileDownloadName;
            _separator = separator;
        }

        protected override void WriteFile(HttpResponseBase response)
        {
            var outputStream = response.OutputStream;
            using (var memoryStream = new MemoryStream())
            {
                WriteList(memoryStream);
                outputStream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
            }
        }

        private void WriteList(Stream stream)
        {
            var streamWriter = new StreamWriter(stream, Encoding.Default);

            WriteHeaderLine(streamWriter);
            streamWriter.WriteLine();
            WriteDataLines(streamWriter);

            streamWriter.Flush();
        }

        private void WriteHeaderLine(StreamWriter streamWriter)
        {
            foreach (MemberInfo member in typeof(T).GetProperties())
            {
                WriteValue(streamWriter, member.Name);
            }
        }

        private void WriteDataLines(StreamWriter streamWriter)
        {
            try
            {
                foreach (T line in _list)
                {
                    foreach (MemberInfo member in typeof(T).GetProperties())
                    {
                        WriteValue(streamWriter, GetPropertyValue(line, member.Name));
                    }
                    streamWriter.WriteLine();
                }
            }
            catch (Exception ex)
            { }
        }


        private void WriteValue(StreamWriter writer, String value)
        {
            writer.Write("\"");
            writer.Write(value.Replace("\"", "\"\""));
            writer.Write("\"" + _separator);
        }

        public static string GetPropertyValue(object src, string propName)
        {
            object value = src.GetType().GetProperty(propName).GetValue(src, null);
            return value != null ? value.ToString() : "";
        }

    }

In my Action Method i wrote following code : 在我的操作方法中,我编写了以下代码:

    public  ActionResult ExportReportToFile(ReportCriteriaViewModels posdata, string name)
            {
  string strQuery = GetReportQuery(posdata, name);
             IEnumerable<REP_MM_DEMOGRAPHIC_CC> lstDemographics = ReportDataAccess.GetReportData<REP_MM_DEMOGRAPHIC_CC>(strQuery);
     return new CsvActionResult<REP_MM_DEMOGRAPHIC_CC>(lstDemographics.ToList(), "LISDataExport.csv");
    }

Using above code it happens nothing... browser doesn't open popup to download/Save file... 使用上面的代码它什么也没有发生...浏览器无法打开弹出窗口来下载/保存文件...

what is the problem in above code and how to solve it ?? 上面的代码有什么问题以及如何解决?

Thanks... 谢谢...

I would not use that code. 我不会使用该代码。 It is homemade and not ready for production usage, in my opinion. 我认为它是自制的,尚未准备好用于生产。

You should definitely take advantage of WebApi (you can use them in your MVC project) and MediaType formatters to return your data in csv format. 您绝对应该利用WebApi(可以在MVC项目中使用它们)和MediaType格式化程序,以csv格式返回数据。

Have a look at this tutorial, that will go through this feature. 看一下本教程,它将经历此功能。

http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM