zoukankan      html  css  js  c++  java
  • gridview 打开word/excel 生成/另存为word/excel

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

    <%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="daochu.aspx.cs" Inherits="test_daochu" %>


    <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="id" ForeColor="#333333" GridLines="None" OnPageIndexChanging="DisplayNews_PageIndexChanging"
    Width="800px" PageSize="50">
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <Columns>
    <asp:BoundField DataField="id" HeaderText="编号" Visible="False" />
    <asp:BoundField DataField="Title" HeaderText="标题">
    <ItemStyle Width="120px" />
    </asp:BoundField>
    <asp:BoundField DataField="cont" HeaderText="内容">
    <ItemStyle Width="500px" />
    </asp:BoundField>
    <asp:BoundField DataField="AddTime" 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>
    View Code
    //后台页面

    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;


    protected void Page_Load(object sender, EventArgs e)
    {
    if( !IsPostBack )
    {
    GRBind();
    }
    }

    //导出表 到 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 )
    private void GRBind()
    {
    //DataSet ds = new DataSet();
    //string strWhere = " Note.typeid = Type.typeid and Note.username = '" + userName + "' ";
    //ds = bn.GetList( strWhere );
    newsDao.newsDao nd=new newsDao.newsDao();
    this.GridView1.DataSource = nd.get_news();
    this.GridView1.DataBind();

    }



  • 相关阅读:
    超棒的监控工具 DataDog Splunk 日志易
    API 接口设计 原则
    程序员 架构师 成长 设计 原则
    OAM 继续演进:阿里云携手微软与 Crossplane 社区发布 OAM Kubernetes 标准实现与核心依赖库
    首席架构师 码农总结 互联网整体解决方案
    《不抱怨的世界2》 读后感
    适合开发者的最佳Linux发行版
    大数据 消息 日志
    CRM 线索来源 获客方式
    微服务开发过程中需要注意的若干事项_逍遥子曰
  • 原文地址:https://www.cnblogs.com/ishibin/p/2312000.html
Copyright © 2011-2022 走看看