Solved

ASP.NET - C# - set gridview cell as text and export to excel as text

Posted on 2012-12-21
8
4,518 Views
Last Modified: 2012-12-31
I am populating a grid with data from a database.  I am then exporting that data to excel.  The problem I am having is that a column will have a value "001" but it exports as "1".  How can I define it so it is text when it exports?

protected void ExportToExcel(DataTable dt)
    {
        string sFileName = "Data_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + ".xls";

        GridView gv = new GridView();
        gv.AllowPaging = false;
        gv.DataSource = dt;
        gv.DataBind();

        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", sFileName));
        Response.ContentType = "application/vnd.ms-excel";
        //Response.ContentType = "application/vnd.openxmlformats";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        //Change the Header Row Foreground to white color
        gv.HeaderRow.Style.Add("color", "#FFFFFF");
        //Applying stlye to gridview header cells
        for (int i = 0; i < gv.HeaderRow.Cells.Count; i++)
        {
            //Change to Blue
            gv.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
        }
        int j = 1;
        //This loop is used to apply stlye to cells based on particular row
        foreach (GridViewRow gvrow in gv.Rows)
        {
            gvrow.BackColor = Color.White;
            if (j <= gv.Rows.Count)
            {
                if (j % 2 != 0)
                {
                    for (int k = 0; k < gvrow.Cells.Count; k++)
                    {
                        gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                    }
                }
            }
            j++;
        }
        gv.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }

Open in new window

0
Comment
Question by:CipherIS
  • 5
  • 2
8 Comments
 
LVL 2

Expert Comment

by:jjester1
ID: 38714210
string myString = myInt.ToString();

also, if you need to pad leading zeros or format, check this info:

http://msdn.microsoft.com/en-us/library/dd260048.aspx
0
 
LVL 1

Author Comment

by:CipherIS
ID: 38714225
Huh?   Where would that go?
0
 
LVL 1

Author Comment

by:CipherIS
ID: 38714446
I walked through the values in the DataTable and GridView and both contain 001.  The problem is when it opens in excel that it formats it to a number.

Any ideas?
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 38715478
Hi, try this

protected void Button1_Click(object sender, EventArgs e)
{
    this.gvw1.Columns[0].HeaderText = "The new header";
}
0
 
LVL 2

Expert Comment

by:jjester1
ID: 38715599
You should be able to format the column in question to be text using:

.EntireColumn.NumberFormat = "@";
0
 
LVL 1

Author Comment

by:CipherIS
ID: 38721211
@jjester1 - where would i put that code ".EntireColumn.NumberFormat = "@"; "

@samirbhogayta - why are you modifying the headertext?
0
 
LVL 1

Accepted Solution

by:
CipherIS earned 0 total points
ID: 38721396
I figured it out.

Original Code
        foreach (GridViewRow gvrow in gv.Rows)
        {
            gvrow.BackColor = Color.White;
            
            if (j <= gv.Rows.Count)
            {
                if (j % 2 != 0)
                {
                    for (int k = 0; k < gvrow.Cells.Count; k++)
                    {
                        gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                    }
                }
            }
            j++;
        }

Open in new window


Code Fix
        foreach (GridViewRow gvrow in gv.Rows)
        {
            gvrow.BackColor = Color.White;

            //HERE IS THE FIX - Format Specific Cell as Text
            gvrow.Cells[21].Style.Add("mso-number-format", "\\@");

            if (j <= gv.Rows.Count)
            {
                if (j % 2 != 0)
                {
                    for (int k = 0; k < gvrow.Cells.Count; k++)
                    {
                        gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                    }
                }
            }
            j++;
        }

Open in new window

0
 
LVL 1

Author Closing Comment

by:CipherIS
ID: 38732187
Figured it out!
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question