468,458 Members | 1,823 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

Leading Zeros truncated when exporting from asp page to excel

9
Hi,

I have the following code where 'recs' is a record set.

For i=0 to recs.Fields.Count - 1
if i = 0 then
pindnt = string(itmlvl*2," ")
response.write pindnt & recs(i)
else
Response.write recs(i)
end if

Response.write CHR(09)
Next

Using this part of code in my application I am exporting data from oracle database to excel, but leading zeros for one column "CURRENT_REVISION" are truncated in the excel sheet.

I am able to see the leading zeros only if I add a string(like ' - single quotes). Please see the below code where I am adding a string.But I don't want any string to the CURRENT_REVISION value in the excel sheet.

For i=0 to recs.Fields.Count - 1
if i = 0 then
pindnt = string(itmlvl*2," ")
response.write pindnt & recs(i)
else
if recs.Fields(i).Name = "CURRENT_REVISION" then
Response.Write "'" & recs(i) ' this is where I concatinated a string
else
Response.write recs(i)
end if
end if
Response.write CHR(09)
Next


My code does not contain any HTML or Javascript code. Using the below code I am exporting to Excel

<%Response.buffer = True
Server.ScriptTimeout = 1800
Response.ContentType ="application/x-msexcel"%>

This seems to be very small issue but eating a lot of my time.
Can anyone help me to resolve this issue.
Dec 3 '07 #1
7 11494
improvcornartist
303 Expert 100+
Excel is probably considering the fields to be numbers, so it removes the leading zero. When you add an extra mark, it then makes the field text, so the zero stays. You could possibly use a decimal at the end of the number (if the field has two decimals, it will be read as text). You may also be able to create your data as a CSV instead of MSExcel, then format the spreadsheet when it is opened in Excel so it will display the version data as text. Otherwise, I'm not really sure how to get around the issue.
Dec 4 '07 #2
leninv
9
Excel is probably considering the fields to be numbers, so it removes the leading zero. When you add an extra mark, it then makes the field text, so the zero stays. You could possibly use a decimal at the end of the number (if the field has two decimals, it will be read as text). You may also be able to create your data as a CSV instead of MSExcel, then format the spreadsheet when it is opened in Excel so it will display the version data as text. Otherwise, I'm not really sure how to get around the issue.

Thank you for the reply. I tried using decimal at the end of the number, but that doesn't work. If I am the only user then I can format the spreadsheet by using CSV, but this will be opened by many users, so I cannot tell every one to format the spreadsheet, so I cannot use CSV instead of MSExcel. I am trying for some alternative using reverse engineering approach. Any how I would like to thank you once again. Any suggestion is always welcome.
Dec 5 '07 #3
leninv
9
Thank you for the reply. I tried using decimal at the end of the number, but that doesn't work. If I am the only user then I can format the spreadsheet by using CSV, but this will be opened by many users, so I cannot tell every one to format the spreadsheet, so I cannot use CSV instead of MSExcel. I am trying for some alternative using reverse engineering approach. Any how I would like to thank you once again. Any suggestion is always welcome.

Hi

I am able to export the content to Excel with Leading zeros now using reverse engineering approach, but I am facing another probelm. The result is displayed on a blank page, I am not getting any cells displayed. I mean no spreadsheet is seen. I used the following statements for Excel

<%Response.buffer = True
Server.ScriptTimeout = 1800
Response.ContentType ="application/vnd.ms-excel"%>

Can you help me to sort out why this problem is coming.
Dec 5 '07 #4
jhardman
3,405 Expert 2GB
I don't know how you got it to work, but the correct way to format numbers as text in excel is to prepend an apostrophe (single quote mark ') to the beginning of the number. Otherwise Excel will always truncate leading zeroes.

Jared
Dec 5 '07 #5
leninv
9
I don't know how you got it to work, but the correct way to format numbers as text in excel is to prepend an apostrophe (single quote mark ') to the beginning of the number. Otherwise Excel will always truncate leading zeroes.

Jared
I tried this but the problem with this is, If I prepend a apostrophe to a value 003 it is showing the result '003. So apostrophe is seen in my result, which I should not get.
Dec 13 '07 #6
This one bit me recently and, finding no solution via Google, I developed my own. I thought I'd share my answer in case it's useful to others.

On columns which are known to be strings that will (or can) contain leading zeroes, do this:

string celltext = "08010203";
celltext = "=concatenate(\"" + celltext + "\")";

Since excel treats the result of a concatenation as a string, even if there was only one element in the concat list, this works. The result appears in excel with the leading zero and with no quotation marks. The obvious disadvantage is the increased number of bytes necessary to represent the data.
Mar 28 '08 #7
sorpigal : this works awesome .... i did it like this : I am working in ASP 1.1

drAddItem(2) = "=concatenate(""" + dtOriginal.Rows(i)(1).ToString() + """)"
or string celltext = "08010203";
celltext = "=concatenate(\"" + celltext + "\")";
Dec 9 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by BARBARA FLOYD | last post: by
5 posts views Thread by samik_tanik | last post: by
1 post views Thread by mmmgood1 | last post: by
6 posts views Thread by Clint Stowers | last post: by
1 post views Thread by kscdavefl | last post: by
6 posts views Thread by JimmyKoolPantz | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.