sharmila k

sharmila k

  • NA
  • 62
  • 51.7k

Export gridview data to formatting width in excel

Nov 5 2013 9:26 AM

Hi friend,

I have some sql data to display in grid view in button click.

S.No

Enquiry_id

Amount_paid

Payment_mode

sl_no

1

1

1000.0000

Check

1

2

2

500.0000

Check

2

3

5

1500.0000

Check

3

4

3

1500.0000

Check

1

5

4

1000.0000

Cash

2

 

I want to export grid view data to excel.

In that grid view remove few column in excel sheet. Using c# code to remove grid view column

Out put

S.No

Amount_paid

sl_no

1

1000.0000

1

2

500.0000

2

3

1500.0000

3

4

1500.0000

1

5

1000.0000

2

 

foreach (GridViewRow drv in GridView1.Rows)

        {

            GridViewRow Temporaryheader = GridView1.HeaderRow;

            GridViewRow Temporaryfooter = GridView1.FooterRow;

            Temporaryheader.Cells[1].Visible = false;

            Temporaryfooter.Cells[1].Visible = false;

            drv.Cells[1].Visible = false;

 

            Temporaryheader.Cells[3].Visible = false;

            Temporaryfooter.Cells[3].Visible = false;

            drv.Cells[3].Visible = false;

 

            //---------

            drv.Cells[1].Width = Unit.Pixel(1);

            drv.Cells[3].Width = Unit.Pixel(5);

            drv.Cells[3].Height = Unit.Pixel(10);

        }

Export Excel:

Response.Clear();

        Response.AddHeader("content-disposition", "attachment;filename=Salary_Details.xls");

        Response.Charset = "";

        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.ContentType = "application/vnd.xls";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());

        Response.End();

 

If I export grid view data to excel, the excel sheet cell width not changed but changed in grid view not export to excel formatting width.

Please anyone help me immediately.

Thanks in Advance.

 


Answers (5)