写入CSV文件并导出? [英] Write to CSV file and export it?

查看:225
本文介绍了写入CSV文件并导出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#ASP.net中,有人可以告诉我如何将数组/列表中的条目写入服务器上的CSV文件,然后打开文件?我认为第二部分将是 - Response.Redirect( http://myserver.com/file.csv ),但不知道如何在服务器上写入文件。



如果这个页面被许多用户访问,最好是生成一个每次新的CSV文件或覆盖相同的文件?如果两个用户尝试访问相同的CSV文件等,会有任何读/写/锁定问题吗?






更新:



这可能是一个愚蠢的问题,我在Google上搜索过,但无法找到确定的答案 - 如何将CSV文件写入网络服务器在C#ASP.net中导出?我知道如何生成它,但我想保存到www.mysite.com/my.csv然后导出。

解决方案

罗姆,你做错了。您不想将文件写入磁盘,以便IIS可以提供​​这些文件。这增加了安全性的影响,并增加了复杂性。您真正需要做的是将CSV直接保存到响应流。



这是场景:用户希望下载csv。用户提交表单,其中包含有关所需csv的详细信息。您准备csv,然后向用户提供一个可以用于构建csv文件并将其写入响应流的aspx页面的URL。用户点击链接。 aspx页面为空白;在页面代码中,您只需将csv写入响应流并结束。



您可以添加以下内容(我相信这是正确的)加载事件:

  string attachment =attachment; filename = MyCsvLol.csv; 
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader(content-disposition,附件);
HttpContext.Current.Response.ContentType =text / csv;
HttpContext.Current.Response.AddHeader(Pragma,public);

var sb = new StringBuilder();
foreach(DataToExportToCSV中的var行)
sb.AppendLine(TransformDataLineIntoCsv(line));

HttpContext.Current.Response.Write(sb.ToString());

写入响应流代码从这里ganked


In C# ASP.net, could someone show me how I can write entries from an Array/List to a CSV file on the server and then open the file? I think the second part would be something like - Response.Redirect("http://myserver.com/file.csv"), however not sure on how to write the file on the server.

Also if this page is accessed by many users, is it better to generate a new CSV file every time or overwrite the same file? Would there be any read/write/lock issues if both users try accessing the same CSV file etc.?


Update:

This is probably a silly question and I have searched on Google but I'm not able to find a definitive answer - how do you write a CSV file to the webserver and export it in C# ASP.net? I know how to generate it but I would like to save it to www.mysite.com/my.csv and then export it.

解决方案

Rom, you're doing it wrong. You don't want to write files to disk so that IIS can serve them up. That adds security implications as well as increases complexity. All you really need to do is save the CSV directly to the response stream.

Here's the scenario: User wishes to download csv. User submits a form with details about the csv they want. You prepare the csv, then provide the user a URL to an aspx page which can be used to construct the csv file and write it to the response stream. The user clicks the link. The aspx page is blank; in the page codebehind you simply write the csv to the response stream and end it.

You can add the following to the (I believe this is correct) Load event:

string attachment = "attachment; filename=MyCsvLol.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");

var sb = new StringBuilder();
foreach(var line in DataToExportToCSV)
  sb.AppendLine(TransformDataLineIntoCsv(line));

HttpContext.Current.Response.Write(sb.ToString());

writing to the response stream code ganked from here.

这篇关于写入CSV文件并导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆