在ASP.Net中使用三个标题行导出到.csv

时间:2017-01-31 06:39:11

标签: c# asp.net csv

我在csv文件中有以下数据:

CustomerID, Name,    Email
1,          Name A,  email1@gmail.com
2,          Name B,  email2@gmail.com
3,          Name C,  email3@gmail.com

导出到.csv文件的代码运行良好。提供如上所述的数据。但我需要像这样更改.csv文件的格式。

CustomerID
Name
Email
1
Name A
email1@gmail.com
CustomerID
Name
Email
2
Name B
email2@gmail.com
CustomerID
Name
Email
3
Name C
email3@gmail.com

以下是我的代码:

 public List<CustomerInfo> GetCustomers()
    {
        List<CustomerInfo> customerList = new List<CustomerInfo>();
        customerList.Add(new CustomerInfo { CustomerID = 1, Name = "Name A", Email = "email1@gmail.com" });
        customerList.Add(new CustomerInfo { CustomerID = 2, Name = "Name B", Email = "email2@gmail.com" });
        customerList.Add(new CustomerInfo { CustomerID = 3, Name = "Name C", Email = "email3@gmail.com" });
        return customerList;
    }


 protected void LinkButton1_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb += sb.Append(string.Format("{0},{1},{2}", "CustomerID", "Name", "Email") + Environment.NewLine);
        List<CustomerInfo> customerList = GetCustomers();
        foreach (CustomerInfo objCustomer in customerList)
        {
            sb.Append(string.Format("{0},{1},{2}", objCustomer.CustomerID.ToString(), objCustomer.Name, objCustomer.Email) + Environment.NewLine);
        }

        byte[] bytes = Encoding.ASCII.GetBytes(sb.ToString());
        if (bytes != null)
        {
            Response.Clear();
            Response.ContentType = "text/csv";
            Response.AddHeader("Content-Length", bytes.Length.ToString());
            Response.AddHeader("Content-disposition", "attachment; filename=\"sample.csv" + "\"");
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
    }

我已经尝试使用+ =只使StringBulder重复下一行,并添加另一个变量byte []。但它仍然无效。它向我展示了sting builder无法用+ =做的错误。也许我做错了。但我不知道如何在csv文件上创建三行标题。我在google上搜索了这么多,但我还是找不到我想要的.csv格式。

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?:

protected void LinkButton1_Click(object sender, EventArgs e)
{
    string headers = "CustomerID" + Environment.NewLine
        + "Name" + Environment.NewLine
        + "Email";
    StringBuilder sb = new StringBuilder();
    List<CustomerInfo> customerList = GetCustomers();
    foreach (CustomerInfo objCustomer in customerList)
    {
        sb.AppendLine(headers);
        sb.AppendLine(objCustomer.CustomerID);
        sb.AppendLine(objCustomer.Name);
        sb.AppendLine(objCustomer.Email);
    }

    byte[] bytes = Encoding.ASCII.GetBytes(sb.ToString());
    if (bytes != null)
    {
        Response.Clear();
        Response.ContentType = "text/csv";
        Response.AddHeader("Content-Length", bytes.Length.ToString());
        Response.AddHeader("Content-disposition", "attachment; filename=\"sample.csv" + "\"");
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
    }
}

虽然说实话,这不再是CSV的常规文字,因此更合适的Content-Type可能是text/plain ...

答案 1 :(得分:0)

基于Nick.McDermaid user1429080评论。我改变了这个。这是工作

 protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string headers = "CustomerID" + Environment.NewLine
                          + "Name" + Environment.NewLine
                          + "Email" + Environment.NewLine;
        StringBuilder sb = new StringBuilder();
        //sb = sb.Append(string.Format("{0},{1},{2}", "CustomerID", "Name", "Email") + Environment.NewLine);

        List<CustomerInfo> customerList = GetCustomers();
        foreach (CustomerInfo objCustomer in customerList)
        {
            //sb.Append(string.Format("{0},{1},{2}", objCustomer.CustomerID.ToString(), objCustomer.Name, objCustomer.Email) + Environment.NewLine);

            sb.Append(headers);
            sb.Append(objCustomer.CustomerID.ToString());
            sb.Append(Environment.NewLine);
            sb.Append(objCustomer.Name);
            sb.Append(Environment.NewLine);
            sb.Append(objCustomer.Email);
            sb.Append(Environment.NewLine);
        }

        byte[] bytes = Encoding.ASCII.GetBytes(sb.ToString());
        if (bytes != null)
        {
            Response.Clear();
            Response.ContentType = "text/csv";
            Response.AddHeader("Content-Length", bytes.Length.ToString());
            Response.AddHeader("Content-disposition", "attachment; filename=\"sample.csv" + "\"");
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
    }

非常感谢你的评论。非常帮助我..:D