Introduction:
将GridView中的数据导出为Excel是web应用中的常见功能。在不同的应用场景下有不同的导出技术。在本文中我将介绍一些导出的技术,希望对您有所帮助
GridView Export the Excel (Basic Code):
.
首先看一个基础的应用。创建一个表格,见截图
<!--[if !vml]--> <!--[if !vml]-->
<!--[endif]-->
然后将数据库中的数据绑定到GridView中的数据,代码如下:
private void BindData()
{
SqlConnection myConnection = new SqlConnection("Server=localhost;Database=School;Trusted_C");
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Users", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
gvUsers.DataSource = ds;
gvUsers.DataBind();
}
<!--[if !vml]-->
<!--[endif]--> <!--[if !vml]--><!--[endif]-->
现在,GridView中已经绑定了数据,接下来的任务就是导出到Excel。下面是button事件中的代码
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvUsers.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
并且还需要override一下 VerifyRenderingInServerForm
方法
(
这一点非常重要,否则在点击按钮后会报错,译者注
)
,
代码如下
:
public override void VerifyRenderingInServerForm(Control control)
{
}
点击导出按钮后会弹出对话框,询问您打开或保存。选择打开文件,导出到Excel的结果如下图:
<!--[if !vml]-->
<!--[endif]--> <!--[if !vml]--><!--[endif]-->
Exporting GridView to Excel With Style:
您是否注意到了以上代码存在一些的问题?是的,ID列开头的0都被截去了。如果你的ID是000345,导出后就编程了345。这个问题可以通过把css添加到输出流中来解决。为了使ID列正确显示,您需要将其储存为文本格式。Excel中的文本格式表示为"mso-number-format:"/@"。
protected void Btn_ExportClick(object sender, EventArgs e)
{
string
style = @"<style> .text { mso-number-format:/@; } </script> ";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvUsers.RenderControl(htw);
// Style is added dynamically
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
在上面的代码中,我通过”style”变量来控制GridView列的样式。并通过Respnose.Write方法将其添加到输出流中。最后把样式添加到ID列。这一步需要在RowDataBound事件中完成
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("class", "text");
}
}
修改的结果如下:
<!--[if !vml]--><!--[endif]--> <!--[if !vml]-->
<!--[endif]-->
将GridView中的数据导出为Excel是web应用中的常见功能。在不同的应用场景下有不同的导出技术。在本文中我将介绍一些导出的技术,希望对您有所帮助
.
首先看一个基础的应用。创建一个表格,见截图
<!--[if !vml]--> <!--[if !vml]-->
![点击预览原图](https://i-blog.csdnimg.cn/blog_migrate/fd6517750118cda7b31cb7753c83b250.gif)
<!--[endif]-->
然后将数据库中的数据绑定到GridView中的数据,代码如下:
private void BindData()
{
SqlConnection myConnection = new SqlConnection("Server=localhost;Database=School;Trusted_C");
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Users", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
gvUsers.DataSource = ds;
gvUsers.DataBind();
}
<!--[if !vml]-->
![](https://i-blog.csdnimg.cn/blog_migrate/bf0e21cb870f8d074432c50e9993e724.gif)
<!--[endif]--> <!--[if !vml]--><!--[endif]-->
现在,GridView中已经绑定了数据,接下来的任务就是导出到Excel。下面是button事件中的代码
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvUsers.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
并且还需要override一下
public override void VerifyRenderingInServerForm(Control control)
{
}
点击导出按钮后会弹出对话框,询问您打开或保存。选择打开文件,导出到Excel的结果如下图:
<!--[if !vml]-->
![](https://i-blog.csdnimg.cn/blog_migrate/d8cfe686672a06d42499f7aca580701c.gif)
<!--[endif]--> <!--[if !vml]--><!--[endif]-->
您是否注意到了以上代码存在一些的问题?是的,ID列开头的0都被截去了。如果你的ID是000345,导出后就编程了345。这个问题可以通过把css添加到输出流中来解决。为了使ID列正确显示,您需要将其储存为文本格式。Excel中的文本格式表示为"mso-number-format:"/@"。
protected void Btn_ExportClick(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvUsers.RenderControl(htw);
// Style is added dynamically
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
在上面的代码中,我通过”style”变量来控制GridView列的样式。并通过Respnose.Write方法将其添加到输出流中。最后把样式添加到ID列。这一步需要在RowDataBound事件中完成
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
修改的结果如下:
<!--[if !vml]--><!--[endif]--> <!--[if !vml]-->
![](https://i-blog.csdnimg.cn/blog_migrate/f57661553dd4f8b38cf1e3526442be4f.gif)
<!--[endif]-->