熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ListView中的內容以Excel導出

2022-06-13   來源: .NET編程 

  <div>

  <asp:ListView ID=ListView runat=server>

  <ItemTemplate>

  <tr>

  <td id=Td runat=server >

  <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=Table runat=server border= >

  <tr runat=server id=itemPlaceholder />

  </table>

  </LayoutTemplate>

  </asp:ListView>

  </div><asp:Button ID=Button runat=server Text=Button

  onclick=Button_Click />

  後台代碼

  public partial class ListView_Code : SystemWebUIPage

  {

  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()

  {

  ListViewDataSource = daList;

  ListViewDataBind();

  }

  private void Export(string FileType string FileName)

  {

  ResponseCharset = GB;

  ResponseContentEncoding = SystemTextEncodingUTF;

  ResponseAppendHeader(ContentDisposition attachment;filename= + HttpUtilityUrlEncode(FileName EncodingUTF)ToString());

  ResponseContentType = FileType;

  thisEnableViewState = false;

  StringWriter tw = new StringWriter();

  HtmlTextWriter hw = new HtmlTextWriter(tw);

  ListViewRenderControl(hw);

  ResponseWrite(twToString());

  ResponseEnd();

  }

  protected void Button_Click(object sender EventArgs e)

  {

  Export(application/msexcel employeexls);

  }

  }


From:http://tw.wingwit.com/Article/program/net/201311/11518.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.