我試圖將我的GridView導出爲2010 Excel文件.xlsx
格式。它適用於.xls
我想知道是否有人可以幫助我。以下是我迄今爲止:將GridView導入到Excel
protected void Button2_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
GridView1.HeaderRow.Cells[0].Style.Add("background-color", "#D1EEEE");
GridView1.HeaderRow.Cells[1].Style.Add("background-color", "#D1EEEE");
GridView1.HeaderRow.Cells[2].Style.Add("background-color", "#D1EEEE");
GridView1.HeaderRow.Cells[3].Style.Add("background-color", "#D1EEEE");
GridView1.HeaderRow.Cells[4].Style.Add("background-color", "#D1EEEE");
GridView1.HeaderRow.Cells[5].Style.Add("background-color", "#D1EEEE");
GridView1.HeaderRow.Cells[6].Style.Add("background-color", "#D1EEEE");
GridView1.HeaderRow.Cells[7].Style.Add("background-color", "#D1EEEE");
GridView1.HeaderRow.Cells[8].Style.Add("background-color", "#D1EEEE");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
row.BackColor = System.Drawing.Color.White;
row.Attributes.Add("class", "textmode");
if (i % 2 != 0)
{
row.Cells[0].Style.Add("background-color", "#FFFFFF");
row.Cells[1].Style.Add("background-color", "#FFFFFF");
row.Cells[2].Style.Add("background-color", "#FFFFFF");
row.Cells[3].Style.Add("background-color", "#FFFFFF");
}
}
GridView1.RenderControl(hw);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
你能展開爲什麼這會有所幫助嗎? – dove
你能提供一些參考文章中的內容嗎?這將大大提高你的答案。 – bytebuster
謝謝!實際上它可以通過使用免費庫的簡單代碼導出數據gridview。 – 2012-11-16 06:18:40