gridview 打开 word,excel 生成,另存为word,excel

来源:互联网 发布:2017甘肃省网络研修 编辑:程序博客网 时间:2024/06/06 12:34

//前台页面   EnableEventValidation = "false"  必须有。。。。。。。。。。。。

<%@ Page Language="C#" MasterPageFile="~/Admin/Admin.Master" EnableEventValidation = "false" AutoEventWireup="true" CodeBehind="daochu.aspx.cs" Inherits="Maticsoft.Web.Admin.daochu" Title="无标题页" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Panel ID="Panel1" runat="server" Height="34px" Width="747px">
        把账本表格导入到<asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Value="excel">Excel</asp:ListItem>
            <asp:ListItem Value="word">word</asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="在线打开"  CommandName="open" OnCommand="Button_Click"/>
        <asp:Button ID="Button2" runat="server" Text="本地保存"  CommandName="save" OnCommand="Button_Click"/></asp:Panel>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        CellPadding="4" DataKeyNames="noteid" ForeColor="#333333" GridLines="None" OnPageIndexChanging="DisplayNews_PageIndexChanging"
        Width="800px" PageSize="50">
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <Columns>
            <asp:BoundField DataField="noteid" HeaderText="编号" Visible="False" />
            <asp:BoundField DataField="typename" HeaderText="消费类别">
                <ItemStyle Width="120px" />
            </asp:BoundField>
            <asp:BoundField DataField="doc" HeaderText="消费详情">
                <ItemStyle Width="500px" />
            </asp:BoundField>
            <asp:BoundField DataField="notetime" HeaderText="消费时间">
                <ItemStyle Width="120px" />
            </asp:BoundField>
            <asp:BoundField DataField="notemoney" HeaderText="消费金额">
                <ItemStyle Width="120px" />
            </asp:BoundField>
           
        </Columns>
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <EditRowStyle BackColor="#999999" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

</asp:Content>

 

//后台页面   重写方法必须有 public override void VerifyRenderingInServerForm(Control control)

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Maticsoft.Web.Admin
{
    public partial class daochu : System.Web.UI.Page
    {
        Maticsoft.Model.Note mn = new Maticsoft.Model.Note();
        Maticsoft.BLL.Note bn = new Maticsoft.BLL.Note();
        protected void Page_Load(object sender, EventArgs e)
        {
            GRBind(Session["username"].ToString());
        }
        //导出表 到 Excel word
        protected void Button_Click(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "save":
                    switch (DropDownList1.SelectedValue)
                    {
                        case "excel":
                            OutPut("attachment;filename=out.xls", "application/ms-excel");
                            break;
                        case "word":
                            OutPut("attachment;filename=out.doc", "application/ms-word");
                            break;

                    }
                    break;
                case "open":
                    switch (DropDownList1.SelectedValue)
                    {
                        case "excel":
                            OutPut("online;filename=out.xls", "application/ms-excel");
                            break;
                        case "word":
                            OutPut("online;filename=out.doc", "application/ms-word");
                            break;

                    }
                    break;
            }
        }
        private void OutPut(string fileType, string strType)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", fileType);
            Response.ContentType = strType;
            this.EnableViewState = false;
            System.IO.StringWriter swOut = new System.IO.StringWriter();
            HtmlTextWriter hTw = new HtmlTextWriter(swOut);
            GridView1.RenderControl(hTw);
            Response.Write(swOut.ToString());
            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        protected void DisplayNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GRBind(Session["username"].ToString());
        }
        private void GRBind(string userName)
        {
            DataSet ds = new DataSet();
            string strWhere = " Note.typeid = Type.typeid  and Note.username =  '" + userName + "' ";
            ds = bn.GetList(strWhere);
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();

        }
    }
}


//  单个导出
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default8 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        string filename = "attachment;filename= 1.doc";
        Response.AppendHeader("Content-Disposition", filename);
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-word";
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.GridView1.AllowPaging = false;
        this.GridView1.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();


    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
}

 

原创粉丝点击