exporting listview data to excel in asp.net

I want to export data from list view.

I have tried the following:

    protected void btnExporttoExcel_Click(object sender, EventArgs e)
    {
       try
        {
            Response.Buffer = true;
            Response.ContentType = "application/x-msexcel";
            Response.AddHeader("Content-Disposition", "attachment; filename=asdf.xls");
            Response.ContentEncoding = Encoding.UTF8;
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            if (dropdown1.SelectedValue=="1")
            {
                listview.RenderControl(hw);
            }
            Response.Write(sw.ToString());
            Response.End();
        }
        catch (Exception ex)
        {
           //catching exception
        }
    }

In this I am obtaining the output but the data are exported as <HTML> tag formats.Help me to export only the data from the list view.

<thead>
    <tr>
    </tr>
    </thead>

              <tr> 
                  <td>data</td>
                  <td>data</td>
                  <td style="text-align:center;">data</td>
                  <td  style="text-align:right;">data</td>
              </tr>      

              <tr> 
                  <td>data</td>
                  <td>data</td>
                  <td style="text-align:center;">data</td>
                  <td  style="text-align:right;">data</td>
              </tr>

              <tr> 
                  <td>data</td>
                  <td>data</td>
                  <td style="text-align:center;">data</td>
                  <td  style="text-align:right;">data</td>
              </tr>

              <tr> 
                  <td>data</td>
                  <td>data</td>
                  <td style="text-align:center;">data</td>
                  <td  style="text-align:right;">data</td>
              </tr>     

please suggest a solution to export only the contents of listview to excel.

Answers


I have used this successfully for GridView but i believe it can same way be used for ListView.

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=asdf.xls");
Response.Charset = "";

Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

listview.DataBind();
if (dropdown1.SelectedValue == "1")
    listview.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

This is my ListView Code

<asp:ListView ID="paidlv" DataSourceID="SqlDataSource1" runat="server" Visible="true">
<LayoutTemplate>
    <table class="detail">
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Telephone</th>
            <th>Email</th>
        </tr>
        <tr id="itemPlaceholder" runat="server"></tr>
    </table>
</LayoutTemplate>
<ItemTemplate>
    <tr class="altRow rowcolor">
        <td><asp:Label runat="server" ID="lblFName" Text='<%#Eval("fname") %>'></asp:Label></td>
        <td><asp:Label runat="server" ID="lblLName" Text='<%#Eval("lname") %>'></asp:Label></td>
        <td><asp:Label runat="server" ID="lblTelephone" Text='<%#Eval("telephone") %>'></asp:Label></td>
        <td><asp:Label runat="server" ID="lblEmail" Text='<%#Eval("email") %>'></asp:Label></td>
    </tr>
</ItemTemplate>
</asp:ListView>

And I suggest you to create a DataTable of data in ListView, then bind that DataTable to new GridView object, and export that GridView object instead.

Here is the sample code:

private DataTable GetDataTable()
{
    DataTable table = new DataTable();
    table.Columns.Add("FirstName"); table.Columns.Add("LastName");
    table.Columns.Add("Telephone"); table.Columns.Add("Email");
    foreach (ListViewItem lvi in paidlv.Items)
    {
        Label lblFName = lvi.FindControl("lblFName") as Label;
        Label lblLName = lvi.FindControl("lblLName") as Label;
        Label lblTelephone = lvi.FindControl("lblTelephone") as Label;
        Label lblEmail = lvi.FindControl("lblEmail") as Label;

        DataRow row = table.NewRow();
        row[0] = lblFName.Text;
        row[1] = lblLName.Text;
        row[2] = lblTelephone.Text;
        row[3] = lblEmail.Text;

        table.Rows.Add(row);
    }

    return table;
}

private void DataTableToExcel(DataTable dataTable)
{
    GridView tempGrid = new GridView();
    tempGrid.AutoGenerateColumns = true;
    tempGrid.DataSource = dataTable;
    tempGrid.DataBind();
    try
    {
        Response.Clear();
        Response.ContentType = "application/application/ms-excel";
        Response.AddHeader("content-disposition", "attachment;filename=asdf.xls");
        Response.ContentEncoding = Encoding.UTF8;
        Response.BinaryWrite(Encoding.UTF8.GetPreamble());
        Response.Charset = "";
        using (StringWriter sw = new StringWriter())
        {
            // You could also write other text here
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                //You could also set cell style here
                if (dropdown1.SelectedValue == "1")
                {
                    tempGrid.RenderControl(hw);
                }
            }
            Response.Write(sw.ToString());
        }
        Response.End();
    }
    catch (Exception ex)
    {
        //catching exception
    }
}

protected void btnExporttoExcel_Click(object sender, EventArgs e)
{ 
   DataTableToExcel(GetDataTable());
}

Need Your Help

Looking for best way to add material design to meteor application

meteor polymer material-design

I am currently building a small meteor based quoting tool application. I would like to add Google's material design to it and and having a hard time to decide what is the best way to do that.

How to get how many days are in the current year?

sqlite

How do I get how many days are in the current year (365,366) using Sqlite?