日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

GridView中的數(shù)據(jù)導(dǎo)出到Excel方法(經(jīng)測(cè)試完整)

 行走在理想邊緣 2019-04-16

protected void Button2_Click(object sender, EventArgs e)
    {
        if (this.GridView1.Rows.Count == 0)
        {
            Response.Write("<script>alert('沒(méi)有查找到數(shù)據(jù),無(wú)法導(dǎo)出!')");
        }
        else
        {
            this.GridView1.AllowPaging = false; // 將有分頁(yè)的GridView中的數(shù)據(jù)全部導(dǎo)出到Excel
            gvBond();
            export("application/ms-excel", "工作人員.xls");
            // 換成 export("application/ms-word", "工作人員.doc"); 那么導(dǎo)出的就是Word格式的了.
            this.GridView1.AllowPaging = true;
            gvBond();
        }
    }
     public void export(string FileType, string FileName)
    {
        string style = @"<style>.text{mso-number-format:/@}</script>";//導(dǎo)入到excel時(shí),保存表里數(shù)字列中前面存在的 0 .
        Response.Clear();
        Response.Charset = "GB2312";
        Response.ContentEncoding = Encoding.UTF7;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        this.GridView1.AllowPaging = false;
        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GridView1.RenderControl(htw);
        Response.Write(style);
        Response.Write(sw.ToString());
        //Response.Write(dt.ToString());
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        //在后臺(tái)中重載VerifyRenderingInServerForm()方法,否則報(bào)錯(cuò)為“類型"GridView"的控件"GridView1"必須放在具有 runat=server 的窗體標(biāo)記內(nèi)“
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[3].Attributes.Add("class", "text");//在數(shù)字列前存在的 0 的列中加入 class 樣式 以便保存 0
        }
    }

注:如果GricView中有分頁(yè)的話,導(dǎo)出到Excel時(shí)就會(huì)報(bào)錯(cuò).可通過(guò)修改頁(yè)文件可以修正這個(gè)問(wèn)題:EnableEventValidation = "false".

<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
請(qǐng)選中你要保存的內(nèi)容,粘貼到此文本框

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多