将gridview数据导出为excel 2007格式(xlsx) [英] Export gridview data to excel 2007 format(xlsx)
本文介绍了将gridview数据导出为excel 2007格式(xlsx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在谷歌搜索。
i希望将数据导出为excel 2007(xlsx)格式,因为它有80,000条记录。
I searched on google.
i want to export data to excel 2007(xlsx) format as it is having 80,000 records.
GridView GridView1 = new GridView();
GridView1.DataSource = ((DataTable)Session["EXECUTIVE_ACT_RPT"]);
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment; filename=abc.xlsx");
//Choose one of these: as the contentType
//Excel 2003 : "application/vnd.ms-excel"
//Excel 2007 : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter html = new System.Web.UI.HtmlTextWriter(writer);
GridView1.RenderControl(html);
Response.Write(writer);
Response.End();
当我打开文件时显示文件无效。
它说:
要么已损坏,要么延期无效
i不想出口它以xls格式。
when i open the file it shows that the file is not valid.
it says :
"either corrupted or the extension is not valid"
i dont want to export it in xls format.
推荐答案
请参阅url
如何将gridview导出到excel 2007 [ ^ ]
Please see url
How to export gridview to excel 2007[^]
请点击以下链接
导出Gridview ASP.NET中的Excel数据 [ ^ ]
谢谢
Please follow the link below
Export Gridview Data to Excel in ASP.NET[^]
Thanks
protected void btnExcel_Click(object sender,ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader(content-disposition,string.Format(attachment; filename = {0},Customers.xlsx));
Response.ContentType =application / vnd.xls
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
//将标题行改回白色
gvdetails.HeaderRow.Style.Add(background-color,#FFFFFF);
/ /将stlye应用于gridview标题单元格
for(int i = 0; i< gvdetails.HeaderRow.Cells.Count; i ++)
{
gvdetails.HeaderRow.Cells [i] .Style.Add(background-color,#507CD1);
}
int j = 1;
//这个循环用于根据特定行将stlye应用于单元格
foreach(gvdetails.Rows中的GridViewRow gvrow)
{
gvrow.BackColor = Color.White;
if(j< = gvdetails.Rows.Count)
{
if(j%2!= 0)
{
for(int k = 0; k< gvrow.Cells.Count; k ++)
{
gvrow.Cells [k] .Style.Add(background-color,#EFF3FB);
} < br $>
}
}
j ++;
}
gvdetails.RenderControl(htw) ;
Response.Write(sw.ToString());
Response.End();
}
请试试这可能对你有帮助....
protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xlsx"));
Response.ContentType = "application/vnd.xls"
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
//Change the Header Row back to white color
gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < gvdetails.HeaderRow.Cells.Count; i++)
{
gvdetails.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
}
int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow gvrow in gvdetails.Rows)
{
gvrow.BackColor = Color.White;
if (j <= gvdetails.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
gvdetails.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
please try this may be help you....
这篇关于将gridview数据导出为excel 2007格式(xlsx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文