PHP Excel won't return correct cell values

Topics: Developer Forum, Project Management Forum, User Forum
Jun 7, 2010 at 9:22 AM

Hi there,

I want to use PHPExcel for reading/writing values to an xlsx file.

I copied the 01simple.xlsx file from de test folder and edited some text and saved the file again. After this I get strange values from PHPExcel.

In cell a1 i have the value 'Hello' but I get the value 'P!b'.

The code I use is:

<?php
require_once("Classes/PHPExcel/IOFactory.php");

$objPHPexcel = PHPExcel_IOFactory::load("01simple.xlsx");
echo "A1 is now: " . $objPHPexcel->getActiveSheet()->getCell('A1')->getValue()."\n";

I use PHP 5.2.4-2ubuntu5.10 with Suhosin-Patch 0.9.6.2 (cli) (built: Jan  6 2010 21:59:29) and the modules I have enabled are:

root@server:~/phpexcel# php5 -m
[PHP Modules]
bcmath
bz2
calendar
ctype
date
dba
dom
exif
filter
ftp
gettext
hash
iconv
json
ldap
libxml
mbstring
mime_magic
mysql
mysqli
ncurses
openssl
pcntl
pcre
PDO
pdo_mysql
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
zip
zlib

[Zend Modules]

I hope you can help me with this strange problem and tell me what I am doing wrong.

Jun 7, 2010 at 9:25 AM

By the way, I use PHPExcel 1.7.3

 

Jun 11, 2010 at 7:34 AM
Is there nobody who know's the answer?
Coordinator
Jun 11, 2010 at 8:01 AM

Without a bit more information, there's not much that we can do.

I've just taken 01simple.xlsx, edited it in Excel and saved, then reloaded in PHPExcel; and everything works as I'd expect.

Can you perhaps upload your modified version of 01simple.xlsx so we can at least see what you changed.

Jun 11, 2010 at 8:42 AM
Thanks for your reply MarkBaker. I uploaded the file: http://phpexcel.codeplex.com/Project/Download/AttachmentDownload.ashx?ProjectName=PHPExcel&WorkItemId=10749&FileAttachmentId=4564 Hope you can find something.
Coordinator
Jun 14, 2010 at 12:46 PM

I'm still having problems identifying any issue with this:

I've used the following script to load your file

error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/');
include 'PHPExcel/IOFactory.php';
?>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Worksheet Bug Test</title>
</head>
<body>
<?php
$fileType = 'Excel2007';
$fileName = 'wesselkranenborg-01simple.xlsx';

try {
 $objPHPExcel = PHPExcel_IOFactory::load($fileName);
} catch (Exception $e) {
 die("Error loading file: ".$e->getMessage()."<br />\n");
}

$sheet = $objPHPExcel->getActiveSheet();
$cells = array('A1','B2','C1','D2','A4','A5');
foreach($cells as $cell) {
   $value = $sheet->getCell($cell)->getValue();
   echo '<b>'.$cell.' Value is </b>'.$value."<br />\n";
}
?>
</body>
</html>

And I'm getting the following results:

A1 Value is Hello
B2 Value is world!
C1 Value is Hello
D2 Value is world!
A4 Value is Miscellaneous glyphs
A5 Value is éàèùâêîôûëïüÿäöüç

The only thing I can think of that might change "Hello" to "P!b" is your display page charset, although even then it should only affect the value of cell A5

 

Jun 14, 2010 at 1:46 PM
Edited Jun 14, 2010 at 1:47 PM
Thanks for your reply.

I tried your script and the result I get is:
A1 Value is PK�����!�b
B2 Value is
C1 Value is
D2 Value is
A4 Value is 
A5 Value is &$

I use the following script for this which is the script which is called from the browser. We use this because our server can't use de php5 apache2-module or cgi version but only the cli version.
<?php
passthru('php5 phpexcel.php');

The content of the phpexcel.php is exactly the script you mentioned above.

When I run the php5 phpexcel.php from the command line of my unix server I get the same output.
Jun 23, 2010 at 8:20 AM
Edited Jun 23, 2010 at 8:20 AM

Has anybody an idea what I do wrong?

Jul 6, 2010 at 7:44 AM

Pls correct me if I am wrong. What I suspect on this issue is Language setting. I think,  you guys are having different language settings. wesselkranenborg, if possible, can you try again after changing your language setting.

Jul 6, 2010 at 9:22 PM

Hi wesselkranenborg!

You have coding problem.

You must use "iconv" function.

example -

$value = $sheet->getCell($cell)->getValue();

$vv=iconv("UTF-8","CP1252","$value");

 

read about "iconv" if you don't know what it is....   :)

good luck!