c \ # gridview 작업: 특정 열 바 인 딩, 엑셀 내 보 내기, WORD 내 보 내기

<%@ 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" onclick="calendar.show(this);" runat="server"></asp:TextBox>
        <span>    </span>
        <asp:TextBox ID="txtEndDate" onclick="calendar.show(this);" runat="server"></asp:TextBox>
        <asp:Button ID="btnSure" runat="server" Text="  " onclick="btnSure_Click" />
        <asp:Button ID="BtnExcel" runat="server" Text="  EXCEL" onclick="BtnExcel_Click" /> 
        <asp:Button ID="BtnWord" runat="server" Text="  WORD" onclick="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>

배경:
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);
        }
    }
}

좋은 웹페이지 즐겨찾기