[英]exporting gridview to Ms Excel 2007
我正在嘗試將網格視圖數據導出到excel 2007,即xlsx格式。 但它給人錯誤。
我正在使用以下代碼
受保護的void Button1_Click(對象發送者,EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=text.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.DataSource = Presenter.CurrentModel.BillStatementGlModelRecords;
GridView1.DataBind();
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
但無法正常工作,並且在打開文件時出現以下錯誤“ Excel無法打開文件'ChangeRequestList [2] .xlsx',因為文件格式或文件擴展名無效。請驗證文件是否未損壞以及文件是否正確擴展名與文件格式匹配”
誰能幫我?
謝謝
看看這段代碼-我已經用過了,並且可以正常工作。 但是它會生成純文本(我猜是csv),而不是xlsx,但是我認為這應該不是問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.