Results 1 to 7 of 7

Thread: how to: vb.net exporting datagrid to excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    2

    how to: vb.net exporting datagrid to excel

    Hi all

    I am exporting the datagrid values to excel. that part is working fine. but when i export the data to excel i am loosing the leading zeros. does any one know how to do this?

    Thanks
    Last edited by toml; Feb 18th, 2004 at 08:03 PM.

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    Sounds like your data is being converted to a numeric data type somewhere along the way. If you can keep it as Text then you should keep the leading zeroes.
    This world is not my home. I'm just passing through.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    2
    can some one give me an example

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Something like this : When it's string type , it should pertain its zeros like trisuglow said .

    VB Code:
    1. Dim I As Integer
    2. Dim Str As String = I.[B]ToString()[/B]

  5. #5
    Lively Member Ksyrium's Avatar
    Join Date
    Jun 2004
    Location
    Tennessee..yes, we CAN code!!
    Posts
    80
    toml,

    I am wanting to export some data out of my datagrid and into excel. Would you mind sharing this with me?

    Thanks in advance!!

  6. #6
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    how are you exporting the data?

    Are you using the excel.application object to open a sheet and writing the data cell by cell, or are you opening a datatable using the oledb provider and connecting to your sheet file?

    There are a few problems with either and it mostly pertains to how excel interprets the data you are sending to the sheet.

    In general excel interprets all numeric data as Numerics, not string, so leading zeros will be truncated unless the column is formatted to display leading zeros.

    It would be helpful to see the code you are using to export your data.
    Whadayamean it doesn't work....
    It works fine on my machine!

  7. #7
    Addicted Member Rockhopper's Avatar
    Join Date
    Aug 2003
    Location
    Cape Town, South Africa
    Posts
    199
    If you are looking for a quick and dirty way to export to excel...

    You can always export in HTML format... excel picks it up, including formatting, but be warned, the grid background dissapears like when u have a graph in excel (because excel is interpreting HTML).
    Then again if you copy the data into a new excel document, the grids work fine.

    In the example, dgrTemp is the datagrid to export

    Code:
     Dim tw As New System.IO.StringWriter
            Dim hw As New System.Web.UI.HtmlTextWriter(tw)
            dgrTemp.RenderControl(hw)
            dgrTemp.Dispose()
    
            Response.ContentType = "application/vnd.ms-excel"
            Response.Charset = " "
            Response.AddHeader("Content-Disposition", "inline;filename=Report.xls;")
            Response.Write("<table border=0>")
            Response.Write("<tr>")
            Response.Write("<td style='font-family:Arial; font-size:12pt; font-weight: lighter'>")
            Response.Write("<b>Report Name</b>")
            Response.Write("<tr></tr>")
            Response.Write("</td>")
            Response.Write("</tr>")
            Response.Write("<tr>")
            Response.Write("<td>")
            Response.Write("Date: " & Date.Now.ToShortDateString & "  Time: " & Date.Now.ToShortTimeString)
            Response.Write("</td>")
            Response.Write("</tr>")
            Response.Write("<td>")
            Response.Write(tw.ToString)
            Response.Write("</td>")
            Response.Write("</tr>")
            Response.Write("</table>")
            Response.End()
    If the facts don't fit the theory, change the facts. --Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width