尝试发送CSV数据时发生PageRequestManagerParserErrorException

时间:2013-02-04 17:52:07

标签: c# asp.net csv

在C#中我打印出csv文件时遇到问题

截至目前,它只是抛出一个错误,它不会打印文件

错误是:

  

Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。

这是我的代码背后,感谢任何帮助,我想我正在做正确的

    protected void csvbutton_Click(object sender, EventArgs e)
    {
        string filename = App_Name_L.ToString();

        StringBuilder sb = new StringBuilder();

        string[] columnNames = new string[]
        {
            "APPLICATION NAME",
            "DESCRIPTION",
            "APPLICATION OWNER",
            "TSO",
            "RESPONSIBLE MANAGER",
            "SIGN OFF",
            "CO-EXISTENCE STATUS",
            "CO-EXISTENCE PROGRESS",
            "MIGRATION PHASE",
            "NEXT STEPS",
            "LAST UPDATE"
        };
        sb.AppendLine(string.Join(",", columnNames));
        sb.AppendLine(Environment.NewLine);

        string[] fields = new string[]
        {
            App_Name_L.Text.ToString(),
            Description_L.Text.ToString(),
            App_Owner_L.Text.ToString().Replace(",", "."),
            TSO_L.Text.ToString().Replace(",", "."),
            Responsible_Manager_L.Text.ToString().Replace(",", "."),
            Sign_Off_L.Text.ToString().Replace(",", "."),
            this.status_export(0, Convert.ToInt16(CS_H.Value.ToString())),
            this.status_export(1, Convert.ToInt16(CP_H.Value.ToString())),
            Migration_Phase_L.Text.ToString(),
            Next_Steps_L.Text.ToString().Replace(",", "."),
            Last_Update_L.Text.ToString()
        };
        sb.AppendLine(string.Join(",", fields));

        stringout.Text = sb.ToString();

        Response.Clear();
        Response.ContentType = "text/csv";
        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + ".csv\"");
        Response.Write(sb.ToString());
        Response.End();
    }

1 个答案:

答案 0 :(得分:0)

Joel这是我刚刚测试过的,它在我的环境中工作,看看这个,看看你是否可以将它应用到你的代码中,并替换datasb.Tostring(); < / p>

private static void ExportToExcel(string data)
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=\"" + filename + ".csv\"");
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
    HttpContext.Current.Response.ContentType = "text/csv";
    HttpContext.Current.Response.Write(data);
    HttpContext.Current.Response.End();
}