欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
c#中從網(wǎng)頁(yè)上gridview導出excel時(shí),如何將excel的列設置成文本類(lèi)型(即保留...

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代碼-------------------------------------------

請問(wèn)導出的時(shí)候為什么沒(méi)有調用Gridview_RowDataBound()這個(gè)函數呢??

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

Ps:第一列是學(xué)號,開(kāi)頭是0。要求是保留前導零即可。。。。

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

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

問(wèn)題補充:

public override void VerifyRenderingInServerForm(Control control)
  {
  }

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

滿(mǎn)意答案 好評率:85%

    /// <summary>
    /// 把Gridview中數據導入到Excel中
    /// </summary>
    /// <param name="gv">需要導出數據的Gridview</param>
    /// <param name="dt">Gridview的數據源</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;//設置導出數據是全部還是部分
        gv.DataSource = dt;
        gv.DataBind();

        //循環(huán)遍歷GridView中的每一列
        for (int i = 0; i < gv.Columns.Count; i++) //設置每個(gè)單元格
        {
            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);

        //預防出現控件必須放在具有 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ù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
從GridView導出Excel
GridView導出到Excel或Word文件
asp.net導出excel并彈出保存提示框
用gridView 綁定數據后導出word文檔
GridView中的數據導出到Excel方法(經(jīng)測試完整)
c#教程之C#實(shí)現GridView導出Excel
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久