9

I'm trying to fix a bug at work where in classic ASP a HTML table is being rendered and then sent to the client as an Excel file. I'll spare the entire source code sample, but essentially we have one column that is alpha numeric, yet when the value starts with one or more zeros, the zeros disappear. I know this is standard Excel behavior for handling numbers, but I want it to treat the value as text. How can I do this?

The Cell In Question:

Response.Write("<td class='tdsmall' align='left' NOWRAP>" & rsPODetail("ITM_ID") & "</td>")

Examples

HTML | EXCEL
00212704 | 212704
00212336 | 212336
00212251 | 212251

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
RSolberg
  • 26,821
  • 23
  • 116
  • 160
  • @jlembke thank you so much for the kind and valuable edit. Unfortunately, that typo isn't as embarrassing as having to work on classic ASP apps built 10 years ago and given no love over the years. – RSolberg Apr 08 '11 at 18:16

6 Answers6

10

Just add one line before your table

Response.Write("<style> TD { mso-number-format:\@; } </style>");

Check this out: Export Gridview to Excel with rows formatted as text

Community
  • 1
  • 1
Arun Singh
  • 1,538
  • 4
  • 19
  • 43
7

Maybe try ="00212704"

Response.Write("<td class='tdsmall' align='left' NOWRAP>=""" & rsPODetail("ITM_ID") & """</td>")
Jon Erickson
  • 112,242
  • 44
  • 136
  • 174
  • Amazing how an apostrophe renders it one way and the double quotes another. The double worked! I had tried this earlier with a single quote and it showed this on output: ='00212704' – RSolberg Apr 08 '11 at 17:30
4

One option that doesn't require modifying the content of the tables is to use the mso-number-format CSS style, as described on this SO answer. For text, you would use something like:

CSS:

.xlText {
    mso-number-format: "\@";
}

HTML:

<td class="xlText">00030</td>
Community
  • 1
  • 1
Cheran Shunmugavel
  • 8,319
  • 1
  • 33
  • 40
1

put style at head of html. TD { mso-number-format:\@; }

all your table cell will convert to text.

0

excel treats a number as text if you put a single quote before the number e.g. '001234

may be this will help you solve your problem

Subhash Lama
  • 433
  • 2
  • 12
  • I did try this. The result was '00212704 being visible rather than 00212704. For some reason Excel didn't hide the apostrophe and kept it visible. – RSolberg Apr 08 '11 at 17:18
0

Can you render it it as a .csv file and download it on the client? Edit: Crap, that doesn't work.

Really, you can't do much of anything that isn't going to be a hack, because IE and Excel don't really "talk" in a standard format. It's a client side issue in Excel that you're trying to override the default behavior on.

Really what you might have to do is create an excel template file, save it to the web server, and write a script that copies the the excel file, makes changes, and sends it to the user. That way you can control the number formatting.

Perplexed
  • 123
  • 7
  • Is the generation of the XLS happening client-side? That can (should?) be moved server-side where he would have full control of the output. – The Matt Apr 08 '11 at 17:26
  • i'm guessing that's exactly the problem - the client is being allowed to interpret the output without being told how to format it. Because the two (HTML and Excel) don't really "talk". Excel just interprets what its opening. – Perplexed Apr 08 '11 at 17:30