我在按钮单击事件的 aspx 页面上使用下面的代码来生成 csv 文件。当我不命名我的文件但尝试使用时,这有效: Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");
要将文件命名为 myfilename.csv,生成的 excel 表是网页的屏幕截图,而不是其中的文本。有人可以帮我解决这个问题。
谢谢!
DataGrid dg = new DataGrid();
dg.DataSource = GetData();
htmlTextWriter.WriteLine("<b>Details</b>");
//Get the html for the control
dg.HeaderStyle.Font.Bold = true;
dg.HeaderStyle.BackColor = System.Drawing.Color.Gray;
dg.DataBind();
dg.RenderControl(htmlTextWriter);
//Write the HTML back to the browser.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
//Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");
this.EnableViewState = false;
Response.Write(textWriter.ToString());
Response.End();
private System.Data.DataTable GetData()
{
System.Data.DataTable dt = new System.Data.DataTable("TestTable");
dt.Columns.Add("SSN");
dt.Columns.Add("Employee ID");
dt.Columns.Add("Member Last Name");
dt.Columns.Add("Member First Name");
dt.Columns.Add("Patient Last Name");
dt.Columns.Add("Patient First Name");
dt.Columns.Add("Claim No.");
dt.Columns.Add("Service Line No.");
dt.Columns.Add("Error Code");
dt.Columns.Add("Error Message");
dt.Rows.Add(123456789,4455,"asdf","asdf","sdfg","xzcv","dsfgdfg123",1234,135004,"some error");
dt.Rows.Add(123456788,3344,"rth","ojoij","poip","wer","aadf124",1233,135005,"Some Error");
dt.Rows.Add(123456787,2233,"dfg","sdfg","vcxb","cxvb","UHCAL125",1223,135006,"another error");
return dt;
}