Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7932459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:01:38+00:00 2026-06-03T21:01:38+00:00

I need to export my gridview data to excel. No problems for this. All

  • 0

I need to export my gridview data to excel. No problems for this.
All works correctly
The problem is i can apply filters to the data displayed in the grid. And when i want to export only those data (once the filter is applied), it exports ALL the data contained previously in my grid before the filter.

I hope i’m clear enough…

This is the code i’m using:

    public void ExportGridToExcel(GridView grdGridView, string fileName)
    {
        Response.Clear();
        Response.Buffer = true;

        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        grdGridView.AllowPaging = false;
        grdGridView.DataBind();

        //Change the Header Row back to white color
        grdGridView.HeaderRow.Style.Add("background-color", "#FFFFFF");

        // Apply style to Individual Cells
        grdGridView.HeaderRow.Cells[0].Style.Add("background-color", "green");
        grdGridView.HeaderRow.Cells[1].Style.Add("background-color", "green");
        grdGridView.HeaderRow.Cells[2].Style.Add("background-color", "green");
        grdGridView.HeaderRow.Cells[3].Style.Add("background-color", "green");
        grdGridView.HeaderRow.Cells[4].Style.Add("background-color", "green");
        grdGridView.HeaderRow.Cells[5].Style.Add("background-color", "green");
        grdGridView.HeaderRow.Cells[6].Style.Add("background-color", "green");

        for (int i = 0; i < grdGridView.Rows.Count; i++)
        {
            GridViewRow row = grdGridView.Rows[i];

            // Change Color back to white
            row.BackColor = System.Drawing.Color.White;
            // Apply text style to each Row
            row.Attributes.Add("class", "textmode");
            // Apply style to Individual Cells of Alternating Row
            if (i % 2 != 0)
            {
                row.Cells[0].Style.Add("background-color", "#C2D69B");
                row.Cells[1].Style.Add("background-color", "#C2D69B");
                row.Cells[2].Style.Add("background-color", "#C2D69B");
                row.Cells[3].Style.Add("background-color", "#C2D69B");
                row.Cells[4].Style.Add("background-color", "#C2D69B");
                row.Cells[5].Style.Add("background-color", "#C2D69B");
                row.Cells[6].Style.Add("background-color", "#C2D69B");
            }
        }
        grdGridView.RenderControl(hw);

        // style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }

What i do to apply filters ( all works perfectly )

    public void refreshGridFactures()
    {
        DateTime? debutFacture = null;
        DateTime? FinFacture = null;
        DateTime? DebutReglement = null;
        DateTime? FinReglement = null;
        int? NumeroFacture = 0;

        if (PickerDateDebut.SelectedDate != null) { debutFacture = PickerDateDebut.SelectedDate; }
        if (PickerDateFin.SelectedDate != null) { FinFacture = PickerDateFin.SelectedDate; }
        if (PickerDebutReglement.SelectedDate != null) { DebutReglement = PickerDebutReglement.SelectedDate; }
        if (PickerFinReglement.SelectedDate != null) { FinReglement = PickerFinReglement.SelectedDate; }
        int IdEntreprise = Convert.ToInt32(ddlEntreprises.SelectedValue);
        string Intitule = txtIntitule.Text;
        if (txtNumeroFacture.Text != "") { NumeroFacture = Convert.ToInt32(txtNumeroFacture.Text); }

        List<DBAccess.Facturation> listFacturation = DBAccess.DAOFacturation.GetFactures(IdEntreprise, debutFacture, FinFacture, DebutReglement, FinReglement, Intitule, NumeroFacture);
        GridFactures.DataSource = listFacturation;
        GridFactures.DataBind();

        double total = Math.Round(listFacturation.Sum(a => a.MontantTTC), 2);
        double totalHT = Math.Round(listFacturation.Sum(a => a.MontantHT), 2);

        lblTotalTTC.Text = "Montant total TTC : " + total;
        lblTotalHT.Text = "Montant total HT : " + totalHT;
    }

Thanks for your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:01:39+00:00Added an answer on June 3, 2026 at 9:01 pm

    Right after your GridViewRow row = grdGridView.Rows[i]; add a check to see if the row is visible

    if(!row.Visible) continue;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to export a gridview to excel, I put the return html code
I need to export data into an Access database. My code works, but it
I need to export some data to excel in my forms aplication, so i
I have a gridview control in ASP.Net 2.0 and i need to export this
I need to export some data from PostgreSQL to Excel (quick customer wish), and
I have a gridview with data. I need to put one button to Export/import
I need to export multiple data tables to Excel on the clients machine, each
I need to export data of all columns (including technical like GUIDs for foreign
I am using MVC3 Web Grid. I need to export the data into excel
I need to export data from php to Excel, and be able to format

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.