c#(Asp.Net) 对 GridView控件操作:实现绑定特定列、导出Excel、导出WORD

25 篇文章 0 订阅

.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="StatisticsProjectTimeWebApp._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script src="js/calendar.js" type="text/javascript"></script>
</head>
<body>
   <form id="form1" runat="server">
    <div>
        <span>开始时间</span>
        <asp:TextBox ID="txtStartDate" οnclick="calendar.show(this);" runat="server"></asp:TextBox>
        <span>结束时间</span>
        <asp:TextBox ID="txtEndDate" οnclick="calendar.show(this);" runat="server"></asp:TextBox>
        <asp:Button ID="btnSure" runat="server" Text="查询" οnclick="btnSure_Click" />
        <asp:Button ID="BtnExcel" runat="server" Text="导出EXCEL" οnclick="BtnExcel_Click" /> 
        <asp:Button ID="BtnWord" runat="server" Text="导出WORD" οnclick="BtnWord_Click" />    
        <asp:GridView ID="GridView1" runat="server" >
        <Columns>
        <asp:TemplateField HeaderText="Brand_ID">
        <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Brand_ID") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="项目名称">
        <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Brand_Name") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="日期">
        <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("CDate") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="时间(毫秒)">
        <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("TotalTime") %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="时间(小时)">
        <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# string.Format("{0:F2}",(double)((long)Eval("TotalTime")/60000)/60) %>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

.cs页后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
 
namespace StatisticsProjectTimeWebApp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BrindData(DateTime.Now, DateTime.Now);
            }        
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
        /// <summary>
        /// 导出EXCEL
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnExcel_Click(object sender, EventArgs e)
        {
            Export("application/ms-excel", "统计项目时间.xls");
        }
 
        private void BrindData(DateTime beginTime,DateTime endTime)
        {
            GridView1.DataSource = SqlHelper.GetData(beginTime,endTime);
            GridView1.AutoGenerateColumns = false;
            GridView1.DataBind();
        }
        /// <summary>
        /// 定义导出Excel的函数
        /// </summary>
        /// <param name="FileType"></param>
        /// <param name="FileName"></param>
        private void Export(string FileType, string FileName)
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }
        /// <summary>
        /// 导出WORD
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnWord_Click(object sender, EventArgs e)
        {
            Export("application/ms-word", "统计项目时间.doc");
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSure_Click(object sender, EventArgs e)
        {
            
            if (txtStartDate.Text== "")
            {
                txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
            }
            if (txtEndDate.Text == "")
            {
                txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
            }
            if (DateTime.Parse(txtStartDate.Text) > DateTime.Parse(txtEndDate.Text))
            {
                txtEndDate.Text = txtStartDate.Text;
            }
           DateTime beginTime= DateTime.Parse( txtStartDate.Text);
           DateTime endTime = DateTime.Parse(txtEndDate.Text);
           BrindData(beginTime, endTime);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值