Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridvew Export to Excel Formatting

Status
Not open for further replies.

schwarem

Programmer
Apr 18, 2002
159
US
I am exporting a gridview to excel. I have a column of ID numbers that are 1.1, 1.2, 1.3, 2.1, 2.2, etc. When it gets up to 1.10 or 2.20, it drops off the zero in excel, because it thinks it is a number not a string. How do I prevent this from happening?
 
you need to make sure your decimal's are set correctly in your DB. what DB are you using?

 
It is set as a string in the database, because it contains both characters and numbers. Some numbers are G.1, G.2, etc. Others are 1.1, 1.2 etc.
 
The best way to get Excel to render your data as you want it to is to write the data out in HTML formatting. You can then have complete control over the output and apply any styles you want. For example, here's a method on how to add formulas to an outputted excel file:


For your problem, if you save an excel file as a html document and set the formatting rules before you save it, you will be able to see what you must output to the Response stream for it to save correctly.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Here is the code I am using to export the gridview to Excel.

Code:
If gvResults.Rows.Count > 0 Then

                gvResults.AllowPaging = False
                gvResults.AllowSorting = False
                gvResults.Columns(0).Visible = False
                PopulateSearch("DateEntered DESC")
                Dim tw As New StringWriter()
                Dim hw As New System.Web.UI.HtmlTextWriter(tw)
                Dim frm As HtmlForm = New HtmlForm()
                Response.ContentType = "application/vnd.ms-excel"
                Response.AddHeader("content-disposition", "attachment;filename=Export.xls")
                Response.Charset = ""
                EnableViewState = False
                Controls.Add(frm)
                frm.Controls.Add(gvResults)
                frm.RenderControl(hw)
                Response.Write(tw.ToString())
                Response.End()
                gvResults.AllowPaging = True
                gvResults.AllowSorting = True
                gvResults.Columns(0).Visible = True
                PopulateSearch("DateEntered DESC")
            End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top