×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

这个好!输出CSV远不如这个来的简单又漂亮。我把code贴出来共享。

本文发表在 rolia.net 枫下论坛判断处理就按照深蓝的办法,很不错。render GridView to excel 的code:
Public void SaveBtn_Click(object sender,EventArgs e)
{
Response.Clear();

Response.AddHeader("content-disposition", "attachment; filename=Locations.xls");

Response.Charset = "";

// If you want the option to open the Excel file without saving than

// comment out the line below

// Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/vnd.xls";

StringWriter stringWrite = new StringWriter();

HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

GridView1.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();
}

Public override void VerifyRenderingInServerForm( Control control)
{
}
但这里有个trick,我曾经屡试屡败,导致最后放弃而使用CSV的办法。上面的代码在我的vs2008里报错,需要在Page directive里关闭EventValidation,像这样:
<%@ Page Language="C#" EnableEventValidation="false" ...>更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 各位长周末愉快吧,请教大家一个GridView输出到CSV文件的问题。
    我目前在项目里是这样的,数据从数据库里bind在GridView上,每个columns的Header都有一个CheckBox,
    用户选中columns后点击输出保存button后就会弹出一个对话框保存/打开CSV文件(Excel可以直接打开,这个方式可能是大多数都喜欢的一个方法,简单容易实现),内容就是选中的columns对应的data,当时赶时间,就硬着头皮用了最笨的办法--印度人的if套if,如果columns多的话简直就太!@#$5,代码极其不容易维护,但一时又想不出好的办法,因为要手工把对应的columns的headers,用","符号把header和header之间做连接,data也是一样,极其郁闷。
    • 这太容易了,.net里都是反射,用字符变量都能搞定,把所有字段名存到list,没选的删,输出每行数据时,从list里取字段名,字段输出。
      • 这个我昨晚也想到过,就用List<string>,看来可行,谢谢。
      • 刚才尝试了一下发现还是不省事
        首先List<string>需要转换成string,才能用StreamWriter.WriteLine(),你提出的后面判断有无checked再remove也麻烦,因为List<>是动态的,按照index来remove容易出错,按照string来remove还有“,"在中间,实际处理并不比我以前用的StringBuilder根据checked columns来append更省事。
    • A simple way is to assign visible of checked column as false, then export the gridview to excel. It automatically filters out invisible columns in excel file.
      The code looks like following:

      Loop throu gridview columns
      Visible of column = ! checkbox.checked;

      Render gridview to excel
      • 这个好!输出CSV远不如这个来的简单又漂亮。我把code贴出来共享。
        本文发表在 rolia.net 枫下论坛判断处理就按照深蓝的办法,很不错。render GridView to excel 的code:
        Public void SaveBtn_Click(object sender,EventArgs e)
        {
        Response.Clear();

        Response.AddHeader("content-disposition", "attachment; filename=Locations.xls");

        Response.Charset = "";

        // If you want the option to open the Excel file without saving than

        // comment out the line below

        // Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.ContentType = "application/vnd.xls";

        StringWriter stringWrite = new StringWriter();

        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());

        Response.End();
        }

        Public override void VerifyRenderingInServerForm( Control control)
        {
        }
        但这里有个trick,我曾经屡试屡败,导致最后放弃而使用CSV的办法。上面的代码在我的vs2008里报错,需要在Page directive里关闭EventValidation,像这样:
        <%@ Page Language="C#" EnableEventValidation="false" ...>更多精彩文章及讨论,请光临枫下论坛 rolia.net
        • public override void VerifyRenderingInServerForm(Control control) { if (!bExport) base.VerifyRenderingInServerForm(control); }
        • public override bool EnableEventValidation { get { if (!bExport) return base.EnableEventValidation; else return false; } set { base.EnableEventValidation = value; } }
          • 行啊,哥们,你这个方法比turn off EventValidation强多了,动态临时turn off,比永久turn off强,起码page上的input validation还能用,多谢!其实这个应该是一个bug,听说在vs2005 release版本里没有?
        • I don’t find any error when rendering gridview to excel in VS 2008.
          I assume that the problem is caused when you embed some controls, such as checkboxes, linkbuttons in your gridview. You can try to remove these controls before rendering. If these controls are in header (or footer), because GridView.Rows are only data rows, you might do it in GridView_RowCreated event after postback.
          • can you share your code, that error is so normal, I wonder how come u didn't get it.