views:

423

answers:

3

Hi,

I'm populating an excel worksheet with the results of a query. In the results set is a 'Description' field which can basically have anything in it, including numbers prefixed with leading zero's, such like 0000234. The problem is that in writing them to a cell, the leading zero's are getting knocked off, so in the case above I end up with 234. How do I ensure that it is treated as a text string and what ever is in the description field is simply pasted in as is?

The line used is:

 Response.Write vbTab & ResultSet(8,r)

Thanks R.

A: 

Prefix the content with an apostrophe.

shahkalpesh
This will prevent Excel from treating the content as a number, but it will also inject a literal apostrophe into the content.
John Y
@John: Yes, I agree. Apologies. I should have tried it first.
shahkalpesh
A: 

If you have a template sheet that you programatically open and fill with data, format the column as Text.

Edo
+2  A: 

Evidently you found a solution to this, given your follow-up question.

For the benefit of anyone else reading this, the solution was to wrap the result in an Excel formula that simply evaluates as a string literal. For example, if your data is 0000234 and you want it to be a 7-character text value, you should give Excel

="0000234"

The ASP code in the original question should be (and was eventually) modified to

Response.Write vbTab & "=""" & ResultSet(8,r) & """"

(Within a string, literal quotation marks must be doubled.)

John Y