tags:

views:

357

answers:

3

I am creating an excel report by changing the content type.

Response.ContentType = "application/vnd.ms-excel"

I have values that contain leading zeros. The issue is when exporting to excel the leading zeros are missing.

e.g.

000123 -> 123

I know that this can be changed manually via excel. The question is how can i accomplish this programmatically?

A: 

Export the column with a leading ' (apostrophe).

Daniel Roseman
This is not the best solution as the column then contains the apostrophe.
Tesseract
+1  A: 

Ive found the answer, you can surround the value in quotes and prefix it with an equals sign to preserve the leading zeros.

="000123"

See here: Excel vs. Leading Zero & Space

Tesseract
+1  A: 

Export your application/vnd.ms-excel as an HTML table, it gives you access to a variety of worksheet formatting options:

Use:

Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);

And send something like the following:

<table border="1">
<caption>This is a caption, it appears centered across all columns</caption>
<colgroup>
<col width="100">
<col width="200">
</colgroup>
<tr valign="bottom">
<td><b>Header 1</b></td>
<td filter="all"><b>Header 2 (filterable)</b></td>
</tr>
<tr valign="top">
<td style="vnd.ms-excel.numberformat:$#,##0.00_)[semicolon]($#,##0.00)">1</td>
<td style="vnd.ms-excel.numberformat:00000000">2</td>
</tr>
<tr valign="top">
<td style="vnd.ms-excel.numberformat:$#,##0_)[semicolon]($#,##0)">3</td>
<td style="vnd.ms-excel.numberformat:00000000">4</td>
</tr>
<tr valign="top">
<td style="vnd.ms-excel.numberformat:$#,##0.00_)[semicolon]($#,##0.00)">-5</td>
<td style="vnd.ms-excel.numberformat:00000000">2</td>
</tr>
<tr valign="top">
<td style="vnd.ms-excel.numberformat:$#,##0.00_)[semicolon][red]$#,##0.00">-7</td>
<td style="vnd.ms-excel.numberformat:00000000">8</td>
</tr>
<tr valign="bottom">
<td formula="=sum(a2:a5)" style="vnd.ms-excel.numberformat:$#,##0.00_)[semicolon][red]$#,##0.00"></td>
<td formula="=average(b2:b5)" style="vnd.ms-excel.numberformat:00000000"></td>
</tr>
</table>

Note that by using the HTML table syntax you can add a column AutoFilter, specify the vertical row alignment, control the width of columns, combine columns with <colspan> (not shown), add formulas and specify the format of column data with vnd.ms-excel.numberformat. You can even do Crosstab (PivotTables) (not shown).

I had a link to more comprehensive documentation about what HTML tags and attributes are supported, and what they do, but I seem to have misplaced it. If anyone else has a good link to Microsoft documentation about this, feel free to edit my answer or include a comment.

EDIT: It seems that you can do a lot more with HTML/XML to build complicated Excel worksheets. I've never gone to these lengths, but it is interesting to know what is possible.

Grant Wagner
Thanks for that. Although my answer servers my purpose this is definitely the correct way to do it.
Tesseract