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

分享

c#中從網(wǎng)頁上gridview導出excel時,如何將excel的列設置成文本類型(即保留...

 gdtoby 2012-05-13

2010-09-27 17:53

protected void Button1_Click(object sender, EventArgs e)
    {


        string style = @"<style> .text { mso-number-format:\@; } </style> ";
        string name = bj + "_" + zy + ".xls";//文件名字
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename="+HttpUtility.UrlEncode(name,System.Text.Encoding.UTF8));
        Response.ContentType = "application/ms-excel";
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
        Response.Write(style);
        Response.Write(sw.ToString());
        Response.End();
    }
    protected void Gridview_RowDataBound(object sender, GridViewRowEventArgs e)
    {      
            e.Row.Cells[1].Attributes.Add("class", "text");
            e.Row.Cells[0].Attributes.Add("class", "text");   
    }

-----------------------------------程序導出excel代碼-------------------------------------------

請問導出的時候為什么沒有調用Gridview_RowDataBound()這個函數(shù)呢??

網(wǎng)上的解決方案我都已經(jīng)試過了,請給出可用的方案。謝謝

Ps:第一列是學號,開頭是0。要求是保留前導零即可。。。。

要求:要么給出此代碼得修改方案,要么給出真實可行的代碼。

請勿灌水,謝謝,,急??!

問題補充:

public override void VerifyRenderingInServerForm(Control control)
  {
  }

此函數(shù)已經(jīng)添加。。。

滿意答案 好評率:85%

    /// <summary>
    /// 把Gridview中數(shù)據(jù)導入到Excel中
    /// </summary>
    /// <param name="gv">需要導出數(shù)據(jù)的Gridview</param>
    /// <param name="dt">Gridview的數(shù)據(jù)源</param>
    /// <param name="strFileName">默認的導出Excel的文件名</param>
    /// <param name="bolPart">全部還是部分導出到Excel.部分:true. 全部:false</param>
    public static void ConvertToExcel(GridView gv, DataTable dt, string strFileName, bool bolPart)
    {
        gv.AllowPaging = bolPart;//設置導出數(shù)據(jù)是全部還是部分
        gv.DataSource = dt;
        gv.DataBind();

        //循環(huán)遍歷GridView中的每一列
        for (int i = 0; i < gv.Columns.Count; i++) //設置每個單元格
        {
            gv.Columns[i].ItemStyle.HorizontalAlign = HorizontalAlign.Left;
            for (int j = 0; j < gv.Rows.Count; j++)
            {
                gv.Rows[j].Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
            }
        }

        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "GB2312";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;

        strFileName += ".xls";
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(strFileName));//設置默認文件名
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

        //預防出現(xiàn)控件必須放在具有 runat=server 的窗體標記內的錯誤
        Page page = new Page();
        HtmlForm form = new HtmlForm();
        gv.EnableViewState = false;
        page.EnableEventValidation = false;
        page.DesignerInitialize();
        page.Controls.Add(form);
        form.Controls.Add(gv);
        page.RenderControl(htw);

        HttpContext.Current.Response.Write(sw.ToString());
        HttpContext.Current.Response.End();
    }

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多