C# 将Gridview导出到Excel:无法导出显示的正确数据

C# 将Gridview导出到Excel:无法导出显示的正确数据,c#,.net,export-to-excel,C#,.net,Export To Excel,我需要将gridview数据导出到excel。这没问题。 一切正常 问题是我可以对网格中显示的数据应用过滤器。当我只想导出这些数据时(一旦应用了过滤器),它会导出过滤器之前网格中包含的所有数据 我希望我足够清楚 这是我正在使用的代码: public void ExportGridToExcel(GridView grdGridView, string fileName) { Response.Clear(); Response.Buffer = t

我需要将gridview数据导出到excel。这没问题。 一切正常 问题是我可以对网格中显示的数据应用过滤器。当我只想导出这些数据时(一旦应用了过滤器),它会导出过滤器之前网格中包含的所有数据

我希望我足够清楚

这是我正在使用的代码:

    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();
    }
public void ExportGridToExcel(GridView grdGridView,字符串文件名)
{
Response.Clear();
Response.Buffer=true;
AddHeader(“内容处置”、“附件;文件名=GridViewExport.xls”);
响应。Charset=“”;
Response.ContentType=“application/vnd.ms excel”;
StringWriter sw=新的StringWriter();
HtmlTextWriter hw=新的HtmlTextWriter(sw);
grdGridView.AllowPaging=false;
grdGridView.DataBind();
//将标题行更改回白色
grdGridView.HeaderRow.Style.Add(“背景色”和“#FFFFFF”);
//将样式应用于单个单元格
grdGridView.HeaderRow.Cells[0].Style.Add(“背景色”、“绿色”);
grdGridView.HeaderRow.Cells[1].Style.Add(“背景色”、“绿色”);
grdGridView.HeaderRow.Cells[2].Style.Add(“背景色”、“绿色”);
grdGridView.HeaderRow.Cells[3].Style.Add(“背景色”、“绿色”);
grdGridView.HeaderRow.Cells[4].Style.Add(“背景色”、“绿色”);
grdGridView.HeaderRow.Cells[5].Style.Add(“背景色”、“绿色”);
grdGridView.HeaderRow.Cells[6].Style.Add(“背景色”、“绿色”);
对于(int i=0;i
我如何应用过滤器(所有操作都很完美)

public void refreshGridFactures()
{
DateTime?debutFacture=null;
DateTime?FinFacture=null;
DateTime?debutreement=null;
DateTime?FinRegElement=null;
int?字符数=0;
如果(PickerDateDefinition.SelectedDate!=null){debutFacture=PickerDateDefinition.SelectedDate;}
如果(PickerDateFin.SelectedDate!=null){FinFacture=PickerDateFin.SelectedDate;}
如果(pickerdebutregelement.SelectedDate!=null){debtreglement=pickerdebutregelement.SelectedDate;}
如果(PickerFinRegElement.SelectedDate!=null){FinRegElement=PickerFinRegElement.SelectedDate;}
int identireprise=Convert.ToInt32(ddlEnterprises.SelectedValue);
字符串Intitule=txtinitule.Text;
if(txtNumeroFacture.Text!=“”){NumeroFacture=Convert.ToInt32(txtNumeroFacture.Text);}
List ListFactoration=DBAccess.DaoFactoration.GetFactories(识别、去特征、去特征、去特征、去特征、去特征、去特征、去特征、去特征、去特征);
GridFactures.DataSource=列表制作;
gridfactes.DataBind();
双倍合计=数学四舍五入(listfacturion.Sum(a=>a.montanttc),2);
double totalHT=数学圆(listfacturion.Sum(a=>a.MontantHT),2);
lbltottalttc.Text=“Montant total TTC:+总计;
lblTotalHT.Text=“Montant total HT:+totalHT;
}

感谢您的帮助

就在您的
gridview行=grdGridView.Rows[i]添加检查以查看行是否可见


如果(!row.Visible)继续

实际上您正在导出为HTML,因为
grdGridView.RenderControl(hw)
将创建一个表;-)
    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;
    }