asp.net中gridview导出excel方法以及怎样修改导出的日期格式
程序员文章站
2024-03-13 12:36:33
...
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="grid_excel_aspnet.aspx.cs"
Inherits="FineUI.Examples.data.grid_excel_aspnet" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" Width="900px" DataKeyNames="Id,Name" AutoGenerateColumns="False"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Container.DataItemIndex + 1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="姓名" />
<asp:TemplateField HeaderText="性别">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# GetGender(Eval("Gender")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EntranceYear" HeaderText="入学年份" />
<asp:CheckBoxField DataField="AtSchool" HeaderText="是否在校" />
<asp:HyperLinkField HeaderText="所学专业" DataTextField="Major" DataTextFormatString="{0}"
DataNavigateUrlFields="Major" DataNavigateUrlFormatString="http://gsa.ustc.edu.cn/search?q={0}"
Target="_blank" />
<asp:ImageField DataImageUrlField="Group" DataImageUrlFormatString="~/res/images/16/{0}.png"
HeaderText="分组">
</asp:ImageField>
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="导出为Excel文件" OnClick="Button2_Click">
</asp:Button>
<br />
<br />
<br />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;
using AspNet = System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace FineUI.Examples.data
{
public partial class grid_excel_aspnet : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
#region BindGrid
private void BindGrid()
{
DataTable table = DataSourceUtil.GetDataTable();
GridView1.DataSource = table;
GridView1.DataBind();
}
#endregion
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
BindGrid();
ResolveGridView(GridView1);
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
Response.ContentEncoding = System.Text.Encoding.UTF8;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
private void ResolveGridView(Control ctrl)
{
for (int i = 0; i < ctrl.Controls.Count; i++)
{
// 图片的完整URL
if (ctrl.Controls[i].GetType() == typeof(AspNet.Image))
{
AspNet.Image img = ctrl.Controls[i] as AspNet.Image;
img.ImageUrl = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, Page.ResolveUrl(img.ImageUrl));
}
// 将CheckBox控件转化为静态文本
if (ctrl.Controls[i].GetType() == typeof(AspNet.CheckBox))
{
Literal lit = new Literal();
lit.Text = (ctrl.Controls[i] as AspNet.CheckBox).Checked ? "√" : "×";
ctrl.Controls.RemoveAt(i);
ctrl.Controls.AddAt(i, lit);
}
if (ctrl.Controls[i].HasControls())
{
ResolveGridView(ctrl.Controls[i]);
}
}
}
}
}
---------------------
上报数据,要求出生日期必须是2001-01-01的格式, 但是导出来的是2001/1/1处理办法
打开控制面板——区域和语言——格式——短日期格式,设置为:yyyy-MM-dd,所有表格就自动OK了!
如图所示: