PHP Classes

Numbers as Text

Recommend this page to a friend!

      Excel Writer  >  All threads  >  Numbers as Text  >  (Un) Subscribe thread alerts  
Subject:Numbers as Text
Summary:It's possible that this class doesn't write the number as text?
Messages:5
Author:Francisco Villegas Athens
Date:2005-09-19 18:31:46
Update:2008-11-15 15:36:53
 

  1. Numbers as Text   Reply   Report abuse  
Picture of Francisco Villegas Athens Francisco Villegas Athens - 2005-09-19 18:31:46
It's possible that this class doesn't write the number as text?

Thanks

Francisco from CHILE

  2. Re: Numbers as Text   Reply   Report abuse  
Picture of Roland Andriese Roland Andriese - 2006-05-18 08:05:33 - In reply to message 1 from Francisco Villegas Athens
Yes this is possible.. quite easy :-)

in the function writeLine in excelwriter.inc.php:

change:
fwrite($this->fp,"<td class=xl24 width=64 >$col</td>");

in:
if (is_int($col))
{
fwrite($this->fp,"<td x:num class=xl24 width=64 >$col</td>");
}
else
{
fwrite($this->fp,"<td class=xl24 width=64 >$col</td>");
}

that is all :-)
good luck

  3. Re: Numbers as Text   Reply   Report abuse  
Picture of Federico Itollo Federico Itollo - 2007-03-27 22:51:40 - In reply to message 2 from Roland Andriese
Any suggestion for floating numbers?
I changed your code in this way:

if (is_numeric($value)) {
fwrite($this->fp,"<td x:num class=xl24 width=64 >$value</td>");
}
else {
fwrite($this->fp,"<td class=xl24 width=64 >$value</td>");
}

For my project I'm not using arrays because I've to collect data from different tables in mysql with different query. That's why I used your code without array.

I had to use is_numeric() beacause is_float() and is_double() don't work. Don't know why... Data have the normal mysql format with "." to separate decimals.

It works fine, but I'd like to set the cell format to have 2 decimal digits (ex. "57.00" and not "57").

Any idea?

  4. Re: Numbers as Text   Reply   Report abuse  
Picture of Claire McMahon Claire McMahon - 2008-06-13 10:19:08 - In reply to message 2 from Roland Andriese
Sincere thanks for this - I was having the same problem and this worked a treat for me.. Claire

  5. Re: Numbers as Text   Reply   Report abuse  
Picture of Geno Takashima Geno Takashima - 2008-11-15 15:36:53 - In reply to message 3 from Federico Itollo
Be careful: is_numeric searches for numeric strings.. so if you want to make it only for NUMBERS, you need to make your own function. I made one that checks whether the given value is integer or float (negative/positive).

You can add this function into excelwriter
function is_number($number) {
if (preg_match('/[^-0-9]/', $number) != preg_match("/^[-0-9]+\\.[0-9]+$/", $number))
return false;
return true;
}

And then modify the line under foreach in the writeLine function to
if ($this->is_date($col) == false && $this->is_number($col) == true) {
fwrite($this->fp,"<td x:num class=xl24 width=64 >$col</td>");
} else {
fwrite($this->fp,"<td class=xl24 width=64 >$col</td>");
}

And the line in writeCol function to
if ($this->is_date($value) == false && $this->is_number($value) == true) {
fwrite($this->fp,"<td x:num class=xl24 width=64 >$value</td>");
} else {
fwrite($this->fp,"<td class=xl24 width=64 >$value</td>");
}

Notice the $this->is_date() part? You can take that out, unless you have dates formatted in the "yyyy-MM-dd" way and you want them to appear as text. Then you need to add the following function too (you can replace the separator '-' if you want to use the function with other stuff):
function is_date($date, $sep = '-') {
if (strlen($date) != 10)
return false;
if (substr($date, 4, 1) != $sep)
return false;
if (substr($date, 7, 1) != $sep)
return false;
return true;
}