Exporting database contents to Excel but missing leading zeroes???

Ask a Question related to Dreamweaver AppDev, Design and Development.

  1. #1

    Default Exporting database contents to Excel but missing leading zeroes???

    I have the following script working well for database exports to Excell.
    However, all my zip codes are missing the leading zeros (probably other
    fields too). What do I need to change in this code to get Excel to accept &
    display leading "0"'s? Thanks!

    <%@ Language=VBScript %>
    <%
    dim Cn,Rs
    set Cn=server.createobject("ADODB.connection")
    set Rs=server.createobject("ADODB.recordset")
    Cn.open "provider=microsoft.jet.oledb.4.0;data source=" &
    server.mappath("database/seminars.mdb")
    Rs.open "select * from Table1",Cn,1,3
    Response.ContentType = "application/vnd.ms-excel"
    Response.AddHeader "Content-Disposition", "attachment;
    filename=LA_Seminars.xls"
    if Rs.eof <> true then
    response.write "<table border=1>"
    response.write "<tr><td>" & "#" & "</td><td>" & "First Name" & "</td><td>" &
    "Last Name" & "</td><td>" & "Title" & "</td><td>" & "Company" & "</td><td>"
    & "Address" & "</td><td>" & "City" & "</td><td>" & "State" & "</td><td>" &
    "Zip Code" & "</td><td>" & "Phone" & "</td><td>" & "Email Address" &
    "</td></tr>"
    while not Rs.eof
    response.write "<tr><td>" & Rs.fields("ID") & "</td><td>" &
    Rs.fields("fname") & "</td><td>" & Rs.fields("lname") & "</td><td>" &
    Rs.fields("title") & "</td><td>" & Rs.fields("company") & "</td><td>" &
    Rs.fields("address") & "</td><td>" & Rs.fields("city") & "</td><td>" &
    Rs.fields("state") & "</td><td>" & Rs.fields("zip") & "</td><td>" &
    Rs.fields("phone") & "</td><td>" & Rs.fields("email") & "</td></tr>"
    Rs.movenext
    wend
    response.write "</table>"
    end if
    set rs=nothing
    Cn.close
    %>


    Burton S. Guest

  2. Similar Questions and Discussions

    1. #40020 [NEW]: int values with leading zeroes
      From: pav at oook dot cz Operating system: FreeBSD PHP version: 5.2.0 PHP Bug Type: SOAP related Bug description: int...
    2. #40020 [Opn->Bgs]: int values with leading zeroes
      ID: 40020 Updated by: tony2001@php.net Reported By: pav at oook dot cz -Status: Open +Status: Bogus...
    3. When exporting to excel leading 0s are omitted
      When I export a datagrid to excel the leading 0s of the first column are truncated. I am using the following code for the export: private void...
    4. Leading zeroes
      Hello, How can I suppress the leading zeroes, except one when all positions are 0; for example: 000015 will be 15 000000 will be 0 thank...
    5. leading zeroes in day, month
      Hello! I would like to show day(date) and month(date) with leading zeroes. Is it any way to do that Mitja
  3. #2

    Default Re: Exporting database contents to Excel but missingleading zeroes???

    the only way i was able to find this is to apply an excel forula to format the
    nubmer as text, which will keep the leading zeros

    =TEXT(0,"<%rs.fields("zip")%>")

    this will by default alight the text to the left, unlike the numbers that get
    aligned to the right. so if that makes a difference to you just add an
    allignment into your html.

    post back if this works for you or if you found a better way of doing this

    nisav19 Guest

  4. #3

    Default Re: Exporting database contents to Excel but missing leading zeroes???

    On 18 May 2005 in macromedia.dreamweaver.appdev, Burton S. wrote:
    > I have the following script working well for database exports to
    > Excell. However, all my zip codes are missing the leading zeros
    > (probably other fields too). What do I need to change in this code
    > to get Excel to accept & display leading "0"'s? Thanks!
    > & "</td><td>" & Rs.fields("state") & "</td><td>" & Rs.fields("zip")
    > & "</td><td>" &
    Well, the ZIP field does look a whole lot like a number...

    Try one or both of two things:
    - cast the zip as a string:
    CStr(Rs.fields("zip"))
    - surround it with quotation marks:
    .... & '</td><td>"' & Rs.fields("zip") & '"</td><td>' & ...

    Make careful note of the location of single and double quotes. I don't
    know if either of these will work, but try them.

    What is the datatype for the 'ZIP' field in your database? Is it
    Text/String, or some sort of Numeric? If it's Numeric, change it to
    String. The only time you want to use a numeric datatype is for data
    you'll be doing arithmetic on, and you'll never do that with a ZIP code.

    --
    Joe Makowiec
    [url]http://makowiec.net/[/url]
    Email: [url]http://makowiec.net/email.php[/url]
    Joe Makowiec Guest

  5. #4

    Post Re: Exporting database contents to Excel but missing leading zeroes???

    Exporting data into excel has sometime some issues. Especially when we want to export numbers. In my case I had one field after export was missing leading zeros. Idea for this is to convert that field into Text and then export it.

    Following is the code: Hope this helps.

    Protected Sub btnExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportToExcel.Click

    Dim dtRecRatesData As DataTable = dsRecRates.Tables(0)
    Dim strFileName As String = ddlDProject.SelectedItem.Value.ToString & "_RecommendedRatesData" & ".xls"

    'Export Rec. Rates data into an Excel
    If Not dtRecRatesData Is Nothing Then

    Try
    Dim style As String = "<style> .text { mso-number-format:\@; } </style> "
    Response.Clear()
    Response.AddHeader("content-disposition", "attachment;filename=" + strFileName + "")
    Response.ContentType = "application/vnd.ms-excel"
    Response.Charset = ""

    Dim textWriter As New IO.StringWriter()
    Dim htmlWriter As New System.Web.UI.HtmlTextWriter(textWriter)

    Dim gvRecRatesData As New DataGrid
    gvRecRatesData.HeaderStyle.BackColor = Drawing.Color.LightGray
    gvRecRatesData.HeaderStyle.ForeColor = Drawing.Color.Blue
    gvRecRatesData.GridLines = GridLines.Both
    gvRecRatesData.HeaderStyle.Wrap = True
    gvRecRatesData.HeaderStyle.Font.Bold = False
    gvRecRatesData.ItemStyle.VerticalAlign = VerticalAlign.Top

    gvRecRatesData.AllowPaging = False
    gvRecRatesData.DataSource = dtRecRatesData 'constructDataSet(dtRecRatesData)
    gvRecRatesData.DataBind()

    'First column which has API12 value, when we were exporting data into excel, it was missing leading zeros.
    'So need to change the cell type to Text so that it will exported properly with leading zeros.
    For cnt As Integer = 0 To gvRecRatesData.Items.Count - 1
    gvRecRatesData.Items(cnt).Cells(0).Attributes.Add( "class", "text")
    Next

    Dim control As Control
    control = gvRecRatesData
    Me.VerifyRenderingInServerForm(control)

    gvRecRatesData.RenderControl(htmlWriter)
    Response.Write(style)
    Response.Write(textWriter.ToString())
    Response.End()

    Catch ex As Exception
    Throw ex
    End Try

    End If
    End Sub
    Unregistered Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139