将gridview数据简单导入到本地Excel中

通常用户导出数据都是导出在本地,应该有像我们保存文件的友好可视化界面供用户自定义文件名和路径。
在.aspx中的代码如下:
 1                      <asp:Button ID="Button2" runat="server" BackColor="ButtonFace" BorderColor="ButtonFace"
 2                          BorderStyle="Outset" OnClick="Button2_Click" Text="导出Excel到" />
 3         <br />
 4          <table  align="center" >
 5             <tr>
 6                 <td style="text-align: center;">
 7            <asp:GridView ID="GridView1" runat="server" BackColor="Silver" BorderColor="#CCCCFF" BorderStyle="None" BorderWidth="1px" CellPadding="1" CellSpacing="1" GridLines="None" Font-Names="宋体"  HorizontalAlign="Left" DataSourceID="CustomersSource" Width="99%"  OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" PageSize="80" >
 8                    <FooterStyle BackColor="SeaShell" ForeColor="Black" />
 9                    <RowStyle ForeColor="Black" BackColor="AliceBlue" Wrap="True" HorizontalAlign="Center" VerticalAlign="Middle" />
10                    <SelectedRowStyle BackColor="LightCyan" Font-Bold="False" ForeColor="Black" />
11                    <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
12                    <HeaderStyle BackColor="#E7E7FF" Font-Bold="False" ForeColor="Black" />
13</asp:GridView>
14        </td></tr></table>
15                         <asp:sqldatasource id="CustomersSource"
16        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
17        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
18        runat="server"/>
19
至于connectionstring,和selectcommand就看你自己要绑定什么数据了。
在.cs文件中的代码如下:
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;
using System.IO;
using System.Text;

public partial class ToExcel : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void Button2_Click(object sender, EventArgs e)
    
{
        Response.Clear();
        Response.Write(
"<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
        Response.AppendHeader(
"content-disposition""attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("中文文件名", System.Text.Encoding.UTF8) + ".xls\"");
        StringWriter sw 
= new StringWriter();
        HtmlTextWriter htw 
= new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

    
public override void VerifyRenderingInServerForm(Control control)
    
{
        
//一定要重载此方法。否则会报错
    }

}

之前参照的方法总是有出现中文乱码的问题,现在使用以下两句是解决中文乱码的方法:
        Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
        Response.AppendHeader(
"content-disposition""attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("中文名", System.Text.Encoding.UTF8) + ".xls\"");

为了方便使用,我在公共调用的方法类PublicUse里为GridView导出Excel写了一个方法:
   /// <summary>
    
/// 将gridview控件内容转换成Excel文件,需重载:public override void VerifyRenderingInServerForm(Control control)
    
/// </summary>
    
/// <param name="Filename">默认的文件名(可为中文)</param>
    
/// <param name="gridview">指定的gridview控件ID</param>
    
/// <param name="page">导出页面,即gridview所在页面,可使用“this.Page”</param>

    public static void ExportToExcel(string Filename, GridView gridview,Page page)
    
{
        page.Response.Clear();
        page.Response.ContentEncoding 
= System.Text.Encoding.GetEncoding("utf-8"); // 中文 
        page.Response.AppendHeader("content-disposition""attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(Filename + DateTime.Now.ToShortDateString(), System.Text.Encoding.UTF8) + ".xls\"");
        StringWriter sw 
= new StringWriter();
        HtmlTextWriter htw 
= new HtmlTextWriter(sw);
        gridview.RenderControl(htw);
        page.Response.Write(sw.ToString());
        page.Response.End();
    }

posted @ 2006-10-12 09:45  '.Elvis.'  阅读(267)  评论(0编辑  收藏  举报