This topic has been archived. It cannot be replied.
-
工作学习 / 学科技术讨论 / 各位长周末愉快吧,请教大家一个GridView输出到CSV文件的问题。我目前在项目里是这样的,数据从数据库里bind在GridView上,每个columns的Header都有一个CheckBox,
用户选中columns后点击输出保存button后就会弹出一个对话框保存/打开CSV文件(Excel可以直接打开,这个方式可能是大多数都喜欢的一个方法,简单容易实现),内容就是选中的columns对应的data,当时赶时间,就硬着头皮用了最笨的办法--印度人的if套if,如果columns多的话简直就太!@#$5,代码极其不容易维护,但一时又想不出好的办法,因为要手工把对应的columns的headers,用","符号把header和header之间做连接,data也是一样,极其郁闷。
-mfcguy();
2008-9-1
{500}
(#4654193@0)
-
这太容易了,.net里都是反射,用字符变量都能搞定,把所有字段名存到list,没选的删,输出每行数据时,从list里取字段名,字段输出。
-googlebot(bot);
2008-9-1
(#4654376@0)
-
这个我昨晚也想到过,就用List<string>,看来可行,谢谢。
-mfcguy();
2008-9-1
(#4655331@0)
-
刚才尝试了一下发现还是不省事首先List<string>需要转换成string,才能用StreamWriter.WriteLine(),你提出的后面判断有无checked再remove也麻烦,因为List<>是动态的,按照index来remove容易出错,按照string来remove还有“,"在中间,实际处理并不比我以前用的StringBuilder根据checked columns来append更省事。
-mfcguy();
2008-9-2
{263}
(#4656420@0)
-
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
-deep_blue(BLUE);
2008-9-2
{131}
(#4656267@0)
-
这个好!输出CSV远不如这个来的简单又漂亮。我把code贴出来共享。
-mfcguy();
2008-9-2
{1124}
(#4656539@0)
-
public override void VerifyRenderingInServerForm(Control control)
{
if (!bExport)
base.VerifyRenderingInServerForm(control);
}
-c1xwy(洪兴罩俺去战斗);
2008-9-2
(#4656558@0)
-
public override bool EnableEventValidation { get { if (!bExport) return base.EnableEventValidation; else return false; } set { base.EnableEventValidation = value; } }
-c1xwy(洪兴罩俺去战斗);
2008-9-2
(#4656586@0)
-
行啊,哥们,你这个方法比turn off EventValidation强多了,动态临时turn off,比永久turn off强,起码page上的input validation还能用,多谢!其实这个应该是一个bug,听说在vs2005 release版本里没有?
-mfcguy();
2008-9-2
(#4656691@0)
-
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.
-deep_blue(BLUE);
2008-9-3
{319}
(#4659427@0)
-
can you share your code, that error is so normal, I wonder how come u didn't get it.
-mfcguy();
2008-9-4
(#4662982@0)