[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Excel export of datagrid is changing a value...

When I use the following function to export the contents of a datagrid to excel, the results are changed when I open the excel document.

For example the field with value '09-4720' will display as 'Sep-20'...  It's doing an auto convert somewhere in the process.

Any ideas of how to keep this change from happening?

Thanks,

Here is the function I'm using to export the data:


    Private Sub Imagebutton4_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Imagebutton4.Click
       
            Dim SE As String
            SE = Session("SortExpression")
            dgdInv.AllowPaging = False
            dgdInv.AllowSorting = False
            BindData(SE)
            Dim stringWrite As New System.IO.StringWriter
            Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
            Dim w As System.Web.UI.WebControls.Unit
            w.Pixel(400)
            dgdInv.Width = w
            Dim scr As String = "<script>function window.onafterprint(){history.back(1);}</script>"
            htmlWrite.Write(scr)
            dgdInv.RenderControl(htmlWrite)
            Dim strHTML As String = stringWrite.ToString()
            Response.Clear()

            ' send to Excel
            Response.AddHeader("Content-Disposition", "attachment;filename=BurdenData.xls")
            Response.ContentType = "application/vnd.ms-excel"
            ' send to Printer
            'Response.ContentType = "text/HTML"

            Response.Write(strHTML)
            Response.Write("<script>window.print();</script>")
            Response.End()

       
    End Sub
0
Weller0123
Asked:
Weller0123
2 Solutions
 
boulder_bumCommented:
You'd have to change the cell formatting, which I believe you'd have to do with the COM API for Excel (which can still be delivered via ASP.NET).

Awesome article to get you started:

http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp
0
 
mmarinovCommented:
Hi Weller0123,

there is no chaning the value through the export. the changing is in the excel when you open it, because the formatting of the cell is General by default and excel tryes to find the format of the cell itself
one way is to manually convert the all column cells to text formatting: select the complete column right click ->Format Cell->from the list select Text
other way, if you want to read the excel file programatically, and there you see the conversion, you have to change the value of this key in the registry
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows
in this key is saved a number of rows that jet engine has to move through to understand what is the type of the hole column
so if your first column is text you can set it to 1 and it will return the correct values

Regards!
B..M
mmarinov
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now