1

I have tried code for saving entire gridview data to an excel file and it is working fine but now i want to save only particular columns of gridview to an excel file.I have used mysql database and asp.net with C#.Please anyone help me with this.

**default.aspx**
 <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>

    <asp:Button ID="btnSave" runat="server" Text="Save to Excel" OnClick="btnSave_Click" />

**default.cs**
 protected void btnSave_Click(object sender, EventArgs e)
    {
        ExportGridToExcel(GridView1, "StudentMarks.xls");
    }

    public void ExportGridToExcel(GridView grdGridView, string fileName)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";
        StringWriter stringWrite = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        grdGridView.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        return;
    }

this is my gridview

this is the excel file which is generated

4

2 に答える 2

0

Excel を生成するには、このメソッドを使用します ..

protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            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);
            GridView.AllowPaging = false;

            // Re-Bind data to GridView 

            using (MSEntities1 CompObj = new MSEntities1())
            {
               // code for bind grid view 
                GridView.DataBind();
            }

            // Change the Header Row back to white color

            GridViewC.Style.Add(" font-size", "10px");

            GridView.HeaderRow.Style.Add("background-color", "#FFFFFF");


            //Apply style to Individual Cells

            GridView.HeaderRow.Cells[0].Style.Add("background-color", "green");
            GridView.HeaderRow.Cells[1].Style.Add("background-color", "green");
            GridView.HeaderRow.Cells[2].Style.Add("background-color", "green");
            GridView.HeaderRow.Cells[3].Style.Add("background-color", "green");
            GridView.HeaderRow.Cells[4].Style.Add("background-color", "green");
            GridView.HeaderRow.Cells[5].Style.Add("background-color", "green");
            GridView.HeaderRow.Cells[6].Style.Add("background-color", "green");
            GridView.HeaderRow.Cells[7].Style.Add("background-color", "green");


            int k = GridView.Rows.Count;

            for (int i = 1; i < GridView.Rows.Count; i++)
            {

                GridViewRow row = GridView.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");
                    row.Cells[7].Style.Add("background-color", "#C2D69B");
                }
            }
            GridView.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();
        }
于 2013-04-15T11:18:29.080 に答える
0

asp:boundfield を使用して、グリッドビューに表示する列を選択できます。次に、そのグリッドビューを Excel にエクスポートします。

あなたの場合、同じデータで新しいグリッドビューをバインドできますが、編集/削除列はありません。この新しいグリッドビューの可視性を false に設定できます。

于 2013-04-15T11:12:19.793 に答える