把GridView导入Excel,把GridView打印出来!

来源:互联网 发布:js btoa函数 编辑:程序博客网 时间:2024/06/11 08:15

一、前台

<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 type="text/javascript">
        function s() {
            var t = document.getElementById("<%=gvGrids.ClientID%>");
            var t2 = t.cloneNode(true);
            for (i = t2.rows.length - 1; i >= 0; i--)
                t2.deleteRow(i)//删掉数据行
            //t2.deleteRow(0)//删掉表头行 
            a.appendChild(t2)
        }
        window.onload = s;
        //打印
        function preview() {
            bdhtml = window.document.body.innerHTML;
            sprnstr = "<!--startprint-->";
            eprnstr = "<!--endprint-->";
            prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
            prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
            window.document.body.innerHTML = prnhtml;
            window.print();
        }
 </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="a" style="height:100px; width:300px; overflow:auto;">       
    </div>
    <asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="导出" />
    <input id="btnExport" type="button" onclick="preview()" value="打印" />
    <table>
    <thead>
    <tr>
    <th colspan="2" style="text-align:center;">基站基本信息</th>
    </tr>
    </thead>
    </table>
    <table>
    <tr>
    <td>
    <!--startprint--> 
    <div id="Div1"  style="overflow:scroll; height:500px; width:815px;">
    <asp:GridView ID="gvGrids" runat="server" AllowPaging="True"
            AutoGenerateColumns="False" onpageindexchanging="gvGrid_PageIndexChanging"
            BackColor="White" PageSize="3" Width="385px"
            onrowcreated="gvGrid_RowCreated">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="身份证号码" >
                </asp:BoundField>
                <asp:BoundField DataField="Sname" HeaderText="姓名" >
                </asp:BoundField>
                <asp:BoundField DataField="Gender" HeaderText="员工性别" >
                </asp:BoundField>
                <asp:BoundField DataField="Addr" HeaderText="家庭地址" >
                </asp:BoundField>
            </Columns>
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />           
        </asp:GridView>
</div>
    <!--endprint-->
</td>
    </tr>
    </table>
    </form>
</body>
</html>

 

 

二、后台

using System;
using System.Data;
using System.Configuration;
//using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
using BLL;

public partial class _Default : System.Web.UI.Page
{
    private Grid grid = new Grid();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BinGrid();
        }
    }
    protected void gvGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //gvGrid.Attributes.Add("style", "table-layout:fixed");
        gvGrids.PageIndex = e.NewPageIndex;
        BinGrid();
    }
    private void BinGrid()
    {
        gvGrids.Attributes.Add("style", "table-layout:fixed");
        gvGrids.DataSource = grid.Grid_Select();
        gvGrids.DataBind();
        gvGrids.DataSource = grid.Grid_Select();
        gvGrids.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Export("application/ms-excel", "学生成绩报表.xls");
    }
    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);
        // turn off paging
        gvGrids.AllowPaging = false;
        BinGrid();

        gvGrids.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();

        //turn the paging on again
        gvGrids.AllowPaging = true;
        BinGrid();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
    protected void gvGrid_RowCreated(object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            e.Row.Cells[i].Attributes.Add("style", "word-break:keep-all;word-wrap:break-word;");//word-wrap:keep-all;
        }
    }
}