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();

}



原文地址:https://www.cnblogs.com/ishibin/p/2312000.html