ASP export to excel problem - excel is changing characters

Posted on 2007-04-10
Last Modified: 2010-08-05

i'm using the following script to export an asp recordset as an excel file :

       Response.ContentType = "application/"
      Response.AddHeader "Content-Disposition", "attachment;filename=reg.xls"
<meta http-equiv="Content-Type" content="application/ charset=windows-1255">
<% set objconn=server.createobject("adodb.connection")
objconn.Open "dsn=xxx"
set objrs=objconn.execute("SELECT * FROM registration")
   '  Loop through each Field, printing out the Field Names
    For i = 0 to objrs.fields.count - 1
varField = objrs(i).name
' varField =  replace(varField,"reg_","")
'varField =  replace(varField,"mailing_","")
    <% next %>
     '  Loop through rows, displaying each field
     while not objrs.eof
    <% For i = 0 to objrs.fields.count - 1
    <TD VALIGN=TOP><% = objrs(i) %></TD>
    <% Next %>


in general this works quite well except i have now come across a problem.... one of the fields in the DB is "card number" which is a credit card number.... in the access db the data is taken from this field is defined as text and displays the info correctly (eg 4670210100354419)...

when this colum is exported to the excel file it is displayed something like this : 4.56345E+15 .... when iselect this number it displays the full number in the formula box at the top but this number is incorrect... in the example where the credit card number recorded in the access db is 4670210100354419 the number shown in the excel would be 4670210100354410 .... as you see the final number is changed to a zero...

does anyone have any suggestions as to how not to export to excel without these problems

many thanks

Question by:dog_star
  • 4
  • 2

Expert Comment

ID: 18881280
There is no problem with your code -this is an excel oddity...
interestingly, excel does this even if cell type is TEXT - it tries to convert to a number, and this is on the edge of excels handling ability.  

The solution is to force excel to look at it as text.  Put some spaces in the numbers (should be ok cos it's a cc number?) eg 4670 2101 0035 4419

This will work fine... as excel is forced to treat is as text

Accepted Solution

TelnetServices earned 250 total points
ID: 18881288
should have mentioned - you can prove this in excel itself - set cell type to "number" "o decimal places" and paste in 4670210100354419 - you will see it comes out as 4670210100354410.

Paste in 4670 2101 0035 4419 - this will work ( as will  #4670210100354419 - but non space characters may affect your data)

Author Comment

ID: 18881690
thanks very much for your answer... not exactly the one i was hoping for, but certainly the correct one :-)

i'm going to try playing round with my export code to try adding the "#" to the beginning of the number as i'd rather not have to go through all my admin pages etc and break the CC number into 4 fields...

again, thanks for your help

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center


Expert Comment

ID: 18881734
To your export code why not use MID to split it up? (final 'right' to catch longer numbers)

strCCnumExport=MID(strccnum,1,4) & " " & MID(strccnum,5,4) & " " & MID(strccnum,9,4) & " " & RIGHT(strccnum,LEN(strccnum)-12)

This is of course, if you want to use spaces - your preference!

You're Welcome! thanx for points

Author Comment

ID: 18881863
thats a very neat solution, far better than manually splitting the cc numbers....

thanks for the code....

Expert Comment

ID: 18881917
np :)

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
replace quotes with UTF-8 character 38 104
Age between date range query (SP) 13 69
ASP Focus problem 3 67
Insert Button on a table 16 36
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question