午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

GridView導(dǎo)入Excel的兩點小技巧?

 悟靜 2012-02-25

GridView導(dǎo)入Excel的兩點小技巧?

VS2005中 GridView導(dǎo)入Excel的導(dǎo)入需要注意的幾點
最近遇到GridView導(dǎo)入Excel問題,總結(jié)出幾點:
1、如果出現(xiàn)下面的錯誤提示可用重載VerifyRenderingInServerForm方法解決。
錯誤提示:
類型“GridView”的控件“GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)
在后臺文件中重載VerifyRenderingInServerForm方法,如:

public override void VerifyRenderingInServerForm(Control control)
{
     //base.VerifyRenderingInServerForm(control);
}


2、如果設(shè)置為 GetEncoding("GB2312"),導(dǎo)出的文件將會出現(xiàn)亂碼。
可用Response.ContentEncoding = System.Text.Encoding.UTF7;
或者Encoding.UTF8等來解決,不過導(dǎo)入格式和字體上個人感覺UTF7比UTF8效果好些;
因人而異了:)

相關(guān)代碼如下:

Web.config配置: <?xml version="1.0"?>
<configuration>
    <appSettings>
        <!--數(shù)據(jù)庫連接串-->
        <add key="ConnectionString" value="data source=.;initial catalog=Northwind;user id=sa;password=sa;persist security info=true;packet size=4096"/>
    </appSettings>
    <connectionStrings/>
    <system.web>
        <compilation debug="true"/>
        <authentication mode="Windows"/>
    </system.web>
</configuration>


ASPX頁面代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www./1999/xhtml" >
<head runat="server">
    <title>無標(biāo)題頁</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="true" >
        </asp:GridView>
    </div>
        <asp:Button ID="Button1" runat="server"  Text="導(dǎo)出到Excel" />
    </form>
</body>
</html>


實例代碼:

/**//*

* //
*/
using System;
using System.Data;
using System.Configuration;
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.Data.SqlClient;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    /**//// <summary>
    /// 鏈接字符串
    /// </summary>
    public string ConnectString
    {
        get
        {
            return ConfigurationManager.AppSettings["ConnectionString"];
        }
    }

    /**//// <summary>
    /// 重載VerifyRenderingInServerForm方法
    /// 確認(rèn)在運行時為指定的 ASP.NET 服務(wù)器控件呈現(xiàn) HtmlForm 控件。
    /// </summary>
    /// <param name="control">ASP.NET 服務(wù)器控件,它必須位于 HtmlForm 控件中</param>
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

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

    /**//// <summary>
    /// 綁定數(shù)據(jù)
    /// </summary>
    public void BindData()
    {
        // 查詢
        string query = "SELECT * FROM Categories";
        SqlConnection myConnection = new SqlConnection(ConnectString);
        SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
        DataSet ds = new DataSet();
        ad.Fill(ds, "Categories");
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    /**//// <summary>
    /// 內(nèi)存分頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Paging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindData();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
        //gaoyang [10/21/2006] 經(jīng)測試如果設(shè)置為 GetEncoding("GB2312"),導(dǎo)出的文件將會出現(xiàn)亂碼。
        Response.ContentEncoding = System.Text.Encoding.UTF7;

        //設(shè)置輸出文件類型為excel文件。
        Response.ContentType = "application/ms-excel";
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.GridView1.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();
    }
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多