热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从c#.net导出到Excel-ExporttoExcelfromc#.net

Exportingfromc#.netIamgettingaproblemIhaveaformthatwhenIexporttoexcelasaresulti

Exporting from c#.net I am getting a problem I have a form that when I export to excel as a result in excel Any ideas why is this happening I am including the ASP code below.

从c#.net导出我遇到问题我有一个表单,当我导出excel导致excel任何想法为什么会发生这种情况我包括下面的ASP代码。

<%@ Page Language="C#" MasterPageFile="~/masterpages/Admin.master" AutoEventWireup="true" CodeFile="members-search-adv.aspx.cs" Inherits="masteradmin_members_search_adv" Title="LISA - Life Insurance Settlement" %>
<%@ Register TagPrefix="UC" TagName="Paging" Src="~/controls/Paging.ascx" %>


    
 Go to Quick Search

Contact Database - Advanced Search

OpenSearch: * 
*Search By Company Name, First Name, Last Name, Tags, Comments
Industry Segment:
Member Type:
Member Rep only:
Board Only:
         <%--split the table here--%>
Suspended Only:
Active Only:
Allow Other Members To See My Info Only:
State:
By Special Level:
Sort by: Company First Name Last Name Membership Type Industry Segment
Export To Excel * Click on the column to SORT
Company First Name Last Name Email

<%--

--%>

Sorry but this is a better explanation of the issue Exporting from c#.net I am getting a problem I have a form that when I export to excel I ge the following

I have data in the datagrid but not in excel I am just getting

对不起,但这是一个更好的解释问题从c#.net导出我遇到问题我有一个表单,当我导出到Excel时我ge以下

我有数据网格中的数据但不是excel我刚刚得到


And this is the function that I am using

这是我正在使用的功能

void ExportToExcel3()
 {
     Response.Clear();
     Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
     Response.Charset = "";
     Response.COntentType= "application/vnd.xls";
     System.IO.StringWriter stringWrite = new System.IO.StringWriter();
     System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
     GridView2.RenderControl(htmlWrite);
     Response.Write(stringWrite.ToString());
     Response.End();
 }



public override void VerifyRenderingInServerForm(Control control)
 {

 }

I am populating the datagrid prior to the export. This is the line of code. It's funny I create a new webform just with this section of code and the export worked. Do you think there's a problem with the datarepeater that I have in the page that is causing the conflict.

我在导出之前填充数据网格。这是代码行。有趣的是,我只使用这部分代码创建了一个新的webform,并且导出工作正常。您是否认为我在页面中导致冲突的datarepeater存在问题。

MemberList list = MemberDB.GetMembers(sql, m_page, m_RecordPerPage, out count, _state);
          this.GridView2.DataSource = list;
          this.GridView2.DataBind();

2 个解决方案

#1


That's not the sequence at all- it's just not how asp.net and the web work.

这根本不是顺序 - 它不是asp.net和网络的工作方式。

You don't fill the grid during the load phase: you retrieve the data and set it as the datasource for the table, but the grid (actually a repeater) isn't filled yet. That doesn't happen until the databinding phase. After the databinding phase (during the render phase) the page with the databound repeater is finally sent by the server to the browser. At this point all your server-side stuff is disposed.

在加载阶段,您没有填充网格:检索数据并将其设置为表的数据源,但网格(实际上是转发器)尚未填充。直到数据绑定阶段才会发生这种情况。在数据绑定阶段之后(在呈现阶段期间),具有数据中继转发器的页面最终由服务器发送到浏览器。此时,所有服务器端的东西都被处理掉了。

Now the browser can finally show the page with the big "Export to Excel" button which the user can click. Again: the server is already destroying all the data you worked so hard to build. When the user clicks your button, a brand new request is created and a brand new page will be sent back to the browser. You have to load all your data into the grid again.

现在,浏览器最终可以显示具有用户可以单击的大“导出到Excel”按钮的页面。再说一次:服务器已经在破坏你工作中难以构建的所有数据。当用户点击您的按钮时,会创建一个全新的请求,并将一个全新的页面发送回浏览器。您必须再次将所有数据加载到网格中。

#2


I found the solution to my problem. Thank you all for contributing. On the Load_Page Event I had the binding inside of an if !postback.

我找到了解决问题的方法。谢谢大家的贡献。在Load_Page事件中,我在if!postback中进行了绑定。


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文探讨了C语言中指针的应用与价值,指针在C语言中具有灵活性和可变性,通过指针可以操作系统内存和控制外部I/O端口。文章介绍了指针变量和指针的指向变量的含义和用法,以及判断变量数据类型和指向变量或成员变量的类型的方法。还讨论了指针访问数组元素和下标法数组元素的等价关系,以及指针作为函数参数可以改变主调函数变量的值的特点。此外,文章还提到了指针在动态存储分配、链表创建和相关操作中的应用,以及类成员指针与外部变量的区分方法。通过本文的阐述,读者可以更好地理解和应用C语言中的指针。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
author-avatar
cfncjl_130
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有