在C#中创建.csv文件

时间:2010-08-20 15:06:08

标签: c# csv export

好吧我想用C#创建一个.csv文件。我一直在四处寻找并注意到很多人正在使用system.IO.memorystream和system.io.streamwriter。

问题在于:我有一个Web应用程序。我想让用户能够导出到excel。问题是,Excel无法安装在服务器上(不要问)。我希望能够为报告编写.csv工作表导出。现在,报告标题和数据对于所有报告都是不同的(循环将解决此问题)。任何人都有一个例子或更好的资源供我使用?

7 个答案:

答案 0 :(得分:13)

这是我通常采用的方法。可能不是最有效的。

        /// <summary>
    /// Generates the contents of the log file.
    /// </summary>
    /// <returns>The contents of the log file.</returns>
    internal string GenerateLogFile()
    {
        StringBuilder csvExport = new StringBuilder();
        csvExport.AppendLine(Resources.CSVHeader);

        foreach (DataRow row in this.logEntries.Rows)
        {
            csvExport.AppendLine(
                string.Format(
                "\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\", \"{9}\"",
                row[ColumnNames.LogTime], row[ColumnNames.Field1], row[ColumnNames.Field2], row[ColumnNames.Field3], row[ColumnNames.Field4], row[ColumnNames.Field5], row[ColumnNames.Field6], row[ColumnNames.Field7], row[ColumnNames.Field8], row[ColumnNames.Field9]));
        }

        return csvExport.ToString();
    }

    /// <summary>
    /// Adds the CSV file to the response.
    /// </summary>
    /// <param name="csvExportContents">The contents of the CSV file.</param>
    internal void DisplayLogFile(string csvExportContents)
    {
        byte[] data = new ASCIIEncoding().GetBytes(csvExportContents);

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=Export.csv");
        HttpContext.Current.Response.OutputStream.Write(data, 0, data.Length);
        HttpContext.Current.Response.End();
    }

答案 1 :(得分:11)

private void WriteItem<T>(StreamWriter sr, T item)
{
    string itemString = item.ToString();
    if(itemString.IndexOfAny(new char[] { '"', ',', '\n', '\r' }) != -1)//skip test and always escape for different speed/filesize optimisation
    {
        sr.Write('"');
        sr.Write(itemString.Replace("\"", "\"\""));
        sr.Write('"');
    }
    else
        sr.Write(itemString);
}
private void WriteLine<T>(StreamWriter sr, IEnumerable<T> line)
{
    bool first = true;
    foreach(T item in line)
    {
        if(!first)
            sr.Write(',');
        first = false;
        WriteItem(sr, item);
    }
}
private void WriteCSV<T>(StreamWriter sr, IEnumerable<IEnumerable<T>> allLines)
{
    bool first = true;
    foreach(IEnumerable<T> line in allLines)
    {
        if(!first)
            sr.Write('\n');
        first = false;
        WriteLine(sr, line);
    }
}
private void WriteCSV<T>(HttpResponse response, IEnumerable<IEnumerable<T>> allLines)
{
    response.ContentType = "text/csv";
    WriteCSV(response.Output, allLines);
}

还可以使用推荐的文件名发送内容处置标头。

编辑:最近,如果需要在枚举中的项之间插入一个动作(如上面的逗号和换行符),我宁愿保留一个继续被检查的布尔值,我直接处理枚举器,然后处理与其余元素分开的第一个元素。我开始将这作为效率推动的微观选择,但已经发展到只是发现它更好地表达了第一项不同的代码路径。因此,我现在将上述内容写成:

private void WriteLine<T>(StreamWriter sr, IEnumerable<T> line)
{
  using(var en = line.GetEnumerator())
  if(en.MoveNext())
  {
    WriteItem(sr, en.Current);
    while(en.MoveNext())
    {
      sr.Write(',');
      WriteItem(sr, en.Current);
    }
}
private void WriteCSV<T>(StreamWriter sr, IEnumerable<IEnumerable<T>> allLines)
{
    using(var en = allLines.GetEnumerator())
    if(en.MoveNext())
    {
      WriteLine(sr, en.Current);
      while(en.MoveNext())
      {
        sr.Write('\n');
        WriteLine(sr, en.Current);
      }
    }
}

答案 2 :(得分:4)

您应该可以使用System.IO.MemoryStreamSystem.IO.StreamWriter使用这些示例。

不是将MemoryStream写入文件,而是将其作为ASP.NET中的ResponseStream返回(确保设置适当的标头值,以便浏览器知道它正在下载文件)。

答案 3 :(得分:4)

只有在使用Office Interop时才需要Excel。使用StringWriter,您只需创建一个文件并添加一些响应信息。只有在打开文件时才需要Excel。

答案 4 :(得分:3)

创建CSV文件真的没有什么神奇之处。 CSV文件的唯一问题是没有真正的标准,大多数导入器只会为导入过程实现一个特定的行为。如果你运气好的话,你会得到一些可以猜得相对较好的东西。

生成CSV文件只是打印行的问题。至于实际细节,请熟悉:

http://en.wikipedia.org/wiki/Comma-separated_values

话虽如此,如果您想要导出到Excel,您可以使用Microsoft的OOXML SDK:

http://msdn.microsoft.com/en-us/library/bb448854.aspx

OOXML SDK是一个C#库,可用于在服务器上生成Excel文件。 Brian Jones博客是如何使用此库的绝佳资源:

http://blogs.msdn.com/b/brian_jones/

答案 5 :(得分:3)

如果您不介意使用第三方库并且LGPL许可证在您的项目中没有问题,那么FileHelpers就是一个很好的工具。

答案 6 :(得分:2)

这是用于创建csv文件的函数:

 private async Task<string> WriteCSV<ViewModel>(IEnumerable<ViewModel> viewModels, string path)
            {
                Type itemType = typeof(ViewModel);
                var props = itemType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                    .OrderBy(p => p.Name);

                var blobName = string.Empty;

                using (var ms = new MemoryStream())
                {

                    using (var writer = new System.IO.StreamWriter(ms))
                    {
                        writer.WriteLine(string.Join(", ", props.Select(p => p.Name)));

                        foreach (var item in viewModels)
                        {

                            writer.WriteLine(string.Join(", ", props.Select(p => p.GetValue(item, null))));
                        }
    }

 }
    }

否则您可以参考以下链接:

https://travelcodingnlotsmore.wordpress.com/2013/06/06/creating-csv-file-from-data-in-list-object-in-c/