zoukankan      html  css  js  c++  java
  • ListView中的内容以Excel导出

    <div>
         
    <asp:ListView ID="ListView1" runat="server">
            
    <ItemTemplate>
                
    <tr>
                    
    <td id="Td1" runat="server" style="">
                        
    <asp:Label ID="idLabel" runat="server" Text='<%# Eval("name") %>' />
                        
    <br />
                    
    </td>
                    
    <td>
                        
    <asp:Label ID="xLabel" runat="server" Text='<%# Eval("Age") %>' />
                        
    <br />
                    
    </td>
                
    </tr>
            
    </ItemTemplate>
            
    <LayoutTemplate>
                
    <table id="Table1" runat="server" border="0" style="">
                    
    <tr runat="server" id="itemPlaceholder" />
                    
                
    </table>
            
    </LayoutTemplate>
        
    </asp:ListView>
        
    </div><asp:Button ID="Button1" runat="server" Text="Button" 
                onclick
    ="Button1_Click" />

    后台代码

    public partial class ListView_Code13 : System.Web.UI.Page
    {
        DataAccess da;
        
    protected void Page_Load(object sender, EventArgs e)
        {
            
    if (Session["da"== null)
            {
                da 
    = new DataAccess();
                Session[
    "da"= da;
            }
            
    else
            {
                da 
    = Session["da"as DataAccess;
            }
            
    if (!IsPostBack)
            {
                Bind();
            }
        }
        
    private void Bind()
        {
            ListView1.DataSource 
    = da.List;
            ListView1.DataBind();
        }
        
    private void Export(string FileType, string FileName)
        {
            Response.Charset 
    = "GB2312";
            Response.ContentEncoding 
    = System.Text.Encoding.UTF7;
            Response.AppendHeader(
    "Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType 
    = FileType;
            
    this.EnableViewState = false;
            StringWriter tw 
    = new StringWriter();
            HtmlTextWriter hw 
    = new HtmlTextWriter(tw);
            ListView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
        }

        
    protected void Button1_Click(object sender, EventArgs e)
        {
            Export(
    "application/ms-excel""employee.xls");

        }
    }

  • 相关阅读:
    python的多线程问题
    python 调用函数时使用星号 *, **
    python正则中的贪婪与非贪婪
    python中文处理
    Python 模块之 ConfigParser: 用 Python 解析配置文件
    substr使用注意
    [转]互联网后台服务的协议设计
    Java设计模式从精通到入门四 工厂方法模式
    logback中logger详解
    logback实践笔记
  • 原文地址:https://www.cnblogs.com/OK_Blog/p/1818407.html
Copyright © 2011-2022 走看看