在Web应用程序中将Gridview导出到Excel [英] Exporting Gridview to Excel in web app

查看:122
本文介绍了在Web应用程序中将Gridview导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望你们可以帮助我。我尝试了很多不同的东西,不能得到这个工作。



我在更新面板中有一个gridview如下:

 < asp:UpdatePanel ID =udpReportrunat =serverUpdateMode =ConditionalChildrenAsTriggers =true> 
< ContentTemplate>
< asp:GridView runat =serverID =preferenceReportGrdAutoGenerateColumns =false
AutoGenerateSelectButton =falseCaptionAlign =TopEnableSortingAndPagingCallbacks =falseHorizo​​ntalAlign =leftCssSelectorClass = gvwPrefReport >
<列>
< asp:BoundField ReadOnly =trueDataField =ClientNameHeaderText =公司名称/>
< asp:BoundField ReadOnly =trueDataField =typeDescriptionHeaderText =首选项/>
< asp:BoundField ReadOnly =trueDataField =defaultValueHeaderText =默认首选项/>
< asp:BoundField ReadOnly =trueDataField =previousPreferenceValueHeaderText =Previous Preference/>
< asp:BoundField ReadOnly =trueDataField =selectedValueHeaderText =新建首选项/>
< asp:BoundField ReadOnly =trueDataField =lastUpdatedHeaderText =上次编辑日期/>
< / Columns>
< / asp:GridView>
< div>
< user:MsgLine runat =serverID =MsgLine1/>
< / div>
< / ContentTemplate>
< / asp:UpdatePanel>

我正在尝试将此gridview导出为ex​​cel。有一个按钮,用户点击它调用该按钮的on_click方法,在这个on_click我有以下:

  string attachment =attachment; filename = Employee.xls; 
Response.AddHeader(content-disposition,附件);
Response.ContentType =application / excel;
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
preferencesReportGrd.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();

我从这个尝试的调试中没有得到任何东西,似乎当我将鼠标悬停在stw.tostring() gridview的值是有的,但没有什么可以写出来。

解决方案

是的。在更新面板标签中,excel导出不起作用。我面对同样的问题。
要解决这个问题,请在更新面板标签的末尾使用以下代码

 < / ContentTemplate> 
<触发器>
< asp:PostBackTrigger ControlID =BtnExport/>
< / Triggers>
< / asp:UpdatePanel>

它甚至在更新面板中也可以


Hopefully you guys can help me out. I tried a lot of different things and cant get this working.

I have a gridview as below in a update panel:

<asp:UpdatePanel ID="udpReport" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
      <ContentTemplate>
          <asp:GridView runat="server" ID="preferenceReportGrd" AutoGenerateColumns="false"
               AutoGenerateSelectButton="false" CaptionAlign="Top" EnableSortingAndPagingCallbacks="false" HorizontalAlign="left" CssSelectorClass="gvwPrefReport">
                   <Columns>
                       <asp:BoundField ReadOnly="true" DataField="ClientName" HeaderText="Company Name" />
                            <asp:BoundField ReadOnly="true" DataField="typeDescription" HeaderText="Preference" />
                            <asp:BoundField ReadOnly="true" DataField="defaultValue" HeaderText="Default Preference" />
                            <asp:BoundField ReadOnly="true" DataField="previousPreferenceValue" HeaderText="Previous Preference" />
                            <asp:BoundField ReadOnly="true" DataField="selectedValue" HeaderText="New Preference" />
                            <asp:BoundField ReadOnly="true" DataField="lastUpdated" HeaderText="Date Last Edited" />
                    </Columns>
          </asp:GridView>
          <div>
              <user:MsgLine runat="server" ID="MsgLine1" />
          </div>
     </ContentTemplate>
</asp:UpdatePanel>

I am trying to export this gridview out to excel. There is a button which the user clicks on it calls the on_click method for that button and in this on_click i have the following:

        string attachment = "attachment; filename=Employee.xls";            
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/excel";
        StringWriter stw = new StringWriter();
        HtmlTextWriter htextw = new HtmlTextWriter(stw);
        preferenceReportGrd.RenderControl(htextw);
        Response.Write(stw.ToString());
        Response.End();

I get nothing from this tried debugging it seems that when i mouse over stw.tostring() all the values for the gridview are there but nothing gets written out.

解决方案

Yes. within the update panel tag excel export is not working. I face the same problem. To solve this use the following code in end of the update panel tag

</ContentTemplate>          
       <Triggers>
        <asp:PostBackTrigger ControlID="BtnExport" />
    </Triggers>
        </asp:UpdatePanel>

it works even within the update panel also

这篇关于在Web应用程序中将Gridview导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆