简体   繁体   English

我正在 Excel 中导出中继器数据,但我不希望中继器中存在的链接按钮显示在 Excel

[英]I am exporting repeater data in Excel, but I don't want linkbuttons present in repeater to display in Excel

I don't want linkbuttons to be shown in the Excel sheet, is there a way to remove them in the class that I have created?我不想在 Excel 表中显示链接按钮,有没有办法在我创建的 class 中删除它们?

I am exporting repeater data into Excel, but I don't want linkbuttons present in repeater to display in Excel.我正在将转发器数据导出到 Excel,但我不希望转发器中存在的链接按钮显示在 Excel 中。

This is my class to export data这是我的 class 导出数据

public void Export(Repeater rpt, string FileName)
{
      HttpContext.Current.Response.Clear();
      HttpContext.Current.Response.Buffer = true;
      HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename="+ FileName +".xls");
      HttpContext.Current.Response.Charset = "";
      HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
      StringWriter sw = new StringWriter();
      HtmlTextWriter hw = new HtmlTextWriter(sw);
      
      HttpContext.Current.Response.Output.Write("<table>");
      sw.Write("<tr>");
      sw.Write("<h3>Company Name<h3>");//Enter Company Name Here
      sw.Write("</tr>");
      sw.Write("<tr>");
      sw.Write("<h3>Address<h3>");//Enter Company Address 
      sw.Write("</tr>");
      sw.Write("<tr>");
      sw.Write("<h3>Contact<h3>");//Enter Company Contact Details 
      sw.Write("</tr>");
      sw.Write("<tr>");
      sw.Write("<h3>Email<h3>");//Enter Companys Emails 
      sw.Write("</tr>");
      #region trying foreach
      List<Repeater> rpts = new List<Repeater>();
     
      //foreach (Control control in rpts)   //remove linkbuttons 
      //{
      //    switch (control.GetType().Name)
      //    {
      //        case "HyperLink":
      //            rpts.Add(new Literal { Text = (control as HyperLink).Text });
      //            break;
      //        case "TextBox":
      //            cell.Controls.Add(new Literal { Text = (control as TextBox).Text });
      //            break;
      //        case "LinkButton":
      //           // rpts.Add(new Literal { Text = (control as LinkButton).Text });
      //            rpts.Add(new Literal { Text = (control as LinkButton).Text });
      //            break;
      //        case "CheckBox":
      //            cell.Controls.Add(new Literal { Text = (control as CheckBox).Text });
      //            break;
      //        case "RadioButton":
      //            cell.Controls.Add(new Literal { Text = (control as RadioButton).Text });
      //            break;

      //    }
      //    cell.Controls.Remove(control);
      //}
      //foreach (Control hlk in rpts)
      //{
      //    if (hlk is LinkButton)
      //    {
             
      //        LinkButton pp = (LinkButton)hlk;
          
      //        pp.Enabled = false;
      //        pp.Visible = false;
      //    }
      //}
      #endregion

      rpt.RenderControl(hw);

      HttpContext.Current.Response.Output.Write(sw.ToString());
      HttpContext.Current.Response.Output.Write("</table>");
      HttpContext.Current.Response.Flush();
      HttpContext.Current.Response.End();
}

This is button click event for exporting这是用于导出的按钮单击事件

protected void btnExport_Click(object sender, EventArgs e)
{
    ShowDetailsInExport s = new ShowDetailsInExport();
    s.Export(Repeater1);
}

If needed this is my ASP.NET repeater creation如果需要,这是我的 ASP.NET 中继器创建

<div class="acrepeater" style="background-color:#efefef;width:500px;height:350px;margin-left:30%">

    <table id="tblSuntech">
        <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="item_Command" >
            <HeaderTemplate>
                <thead >
                    <th style="border:1PX solid">Action</th>
                    <th style="border:1PX solid">CountryId</th>
                    <th style="border:1PX solid">CountryName</th>
                    <th style="border:1PX solid">Status</th>
                    <th style="border:1PX solid">Action</th>
                    <th ></th>

                </thead>
            </HeaderTemplate>
            <ItemTemplate>
                <%--OnClientClick="EditPop();return false;"--%>
                <tr class="tr">
                    <td style="border:1PX solid">
                 <asp:LinkButton ID="LinkButton3" CssClass="tbl-id-btn" runat="server" 
                     
                     ToolTip="View" CausesValidation="false" CommandName="DashBoard" 
                     CommandArgument='<%#Eval("CountryId")+","+ Eval("CountryName")%>' ForeColor="#0066FF">Edit</asp:LinkButton>
            </td>
                    <td style="border:1PX solid"><%# Eval("CountryId")%></td>
                    <td style="border:1PX solid"><%# Eval("CountryName")%></td>
                    <td style="border:1PX solid"><%# Eval("Status")%></td>

                    <td style="border:1PX solid">
         
                        <asp:LinkButton ID="LinkButton1" CssClass="tbl-id-btn" style="width:100px" runat="server" 
                     
                     ToolTip="View" CausesValidation="false" CommandName="IsBlock" 
                     CommandArgument='<%# Eval("[CountryId]")+","+Eval("Status")%>' ForeColor="Red"><%# Eval("[StatusMessage]")%></asp:LinkButton>
                        </td>
                   
            </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>
</div>

Every object in programming that displays a list of data needs a data source.每个 object 在显示数据列表的编程中都需要一个数据源。 This data source may be Datatable, Dataset Datatable, Linq List or any other type.该数据源可以是 Datatable、Dataset Datatable、Linq List 或任何其他类型。 All of these return a list, and this list can be represented by those objects.所有这些都返回一个列表,并且该列表可以由这些对象表示。 Before displaying the list, you can perform your desired operations (eg Sorting, Editing, Deleting, etc.) on the received list.在显示列表之前,您可以对接收到的列表进行所需的操作(例如排序、编辑、删除等)。 You can also use the same list to send to Excel output.您也可以使用相同的列表发送到 Excel output。 When you convert an object such as DataRepeater to Excel, a number of other objects with that object may be sent to Excel output. When you convert an object such as DataRepeater to Excel, a number of other objects with that object may be sent to Excel output. But when you send a pure list of data, such as a Datatable, to the Excel file creation method, in fact, only the sorted list submitted is converted to an Excel file, and the problems you mentioned no longer arise.但是当你向Excel文件创建方法发送一个数据表之类的纯数据列表时,实际上只是将提交的排序列表转换为Excel文件,就不再出现你提到的问题了。 I hope I was able to guide you with my own explanation.我希望我能够用我自己的解释来指导你。 If you need more help please inform me.如果您需要更多帮助,请通知我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM