打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c#中从网页上gridview导出excel时,如何将excel的列设置成文本类型(即保留...

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()这个函数呢??

网上的解决方案我都已经试过了,请给出可用的方案。谢谢

Ps:第一列是学号,开头是0。要求是保留前导零即可。。。。

要求:要么给出此代码得修改方案,要么给出真实可行的代码。

请勿灌水,谢谢,,急!!

问题补充:

public override void VerifyRenderingInServerForm(Control control)
  {
  }

此函数已经添加。。。

满意答案 好评率: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();

        //循环遍历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);

        //预防出现控件必须放在具有 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();
    }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
从GridView导出Excel
GridView导出到Excel或Word文件
asp.net导出excel并弹出保存提示框
用gridView 绑定数据后导出word文档
GridView中的数据导出到Excel方法(经测试完整)
c#教程之C#实现GridView导出Excel
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服