1

I'm using the following code to render a gridview to an XLS file on an ASPX page:

Private Sub ExportGridviewToExcel()
    Dim attachment As String = "attachment; filename=ItemSizeExport.xls"
    Response.ClearContent()
    Response.AddHeader("content-disposition", attachment)
    Response.ContentType = "application/ms-excel"
    Dim sw As New StringWriter()
    Dim htw As New HtmlTextWriter(sw)
    GV_Item_Data.RenderControl(htw)
    Response.Write(sw.ToString())
    Response.End()
End Sub

One of the columns is usually null, but sometimes contains a rather long integer such as 2342515123332222 .. it's a barcode type number... so no math ever has to be done on it.

Long story short, XLS makes this into a floating point and that's junk for the end-user. (~23432E18)

Is there some way for me to force this column in the XLS to be a string?

I'm not willing to generate an XLS via any other method... my "fall back" would be to write a '#' character before each non-blank blank entry in that column hoping that that forces the column to be interpreted as text rather than a number. This is not an ideal solution though, unfortunately.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
  • 1
    Im pretty sure thats just due to the formating of that cell, but not too sure how to go about changing that programatically... – dkarzon Oct 27 '09 at 22:05

1 Answers1

1

Instead of a # character before each non-blank entry in that column, you could use an apostrophe. That will convert it to text. Another approach that might be possible is to attach a macro with column format info, but I'm not sure whether that's possible.

xpda
  • 15,585
  • 8
  • 51
  • 82
  • I tried the apostrophe, but for whatever reason Excel doesn't process it and simply displays it. However, in Excel, if I go in to edit one of those cells (with an apostrophe already prefixed), it "processes" it and it becomes a text cell as intended and removes the apostrophe. – Brian Webster Oct 29 '09 at 17:59
  • There does not appear to be a clear answer on this one, so I'm accepting this one because it is informative. – Brian Webster Oct 30 '09 at 18:45