By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,156 Members | 1,069 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,156 IT Pros & Developers. It's quick & easy.

Removing Trailing spaces from Excel using asp.net

P: n/a
Ben


Hi All,
I have an Excel issue which I am unable to solve for the past couple of
days.
I have a printable version of a report , which uses the basic
Response.ContentType="application/vnd.ms-excel"; to give the report in
excel.
Now one of the fields is numeric, but Excel adds a trailing space to
every number, hence that column does not give a sum. Is there any way to
remove these trailing spaces via .net?
Thanks,
Ben

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
The best way to do this is to tell Excel the cell is a numeric value by
adding the format attribute x:num to the TD tag:
<td x:num> 42 </td>

- Jon
http://weblogs.asp.net/jgalloway

Nov 19 '05 #2

P: n/a
Ben


To give you a more clear pciture of my problem....
I have an aspx page with a repeater and the datasourse is a datatable,
since i have a simeple html output and an excel output, i only specify
the application type, depending on which option the user wants to see.So
my question is...how to do i specify in my code to format the cells of
the worksheet?
Thanks,
Ben

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #3

P: n/a
Here's an example:

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr bgcolor="#ffcc99">
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Salary</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr bgcolor="#ffcccc">
<td><%# DataBinder.Eval(Container.DataItem, "au_id") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "au_fname") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "au_lname") %></td>
<td x:num><%# DataBinder.Eval(Container.DataItem, "salary")
%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

Note that the salary field td tag includes x:num.
- Jon
http://weblogs.asp.net/jgalloway

Nov 19 '05 #4

P: n/a
Ben


Tried adding x:num formatting to the table....doe snot remove the
trailing spaces. Also if i see the format cells, it says general and not
numeric.Any idea?
Thanks,
Ben

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #5

P: n/a
If you're mostly concerned about trimming spaces, you can just use the
string.Trim() method:

<%# Convert.ToString(DataBinder.Eval(Container.DataIte m,
"salary")).Trim() %>

For the x:num formatting to work, it needs to be applied to every table
cell (<td>), not to the entire table. Also, I think your HTML tag on
the ASPX page needs to include the excel namespace:
<html xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

Sorry I left that out before - I can't find my sample code that does
this.

- Jon
http://weblogs.asp.net/jgalloway

Nov 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.