MAXakaWIZARD / xls-writer

Port of PEAR Spreadsheet Excel Writer
3 stars 2 forks source link

Number formats don't seem to work #2

Closed themolecule closed 9 years ago

themolecule commented 9 years ago

Number formats don't seem to carry through to the xls file...

This should produce cells with currency formatting:

<?

require( dirname(__FILE__)."/include/xls-writer-master/vendor/autoload.php" );

use Xls\Workbook;
use Xls\Biff8;

$workbook = new Xls\Workbook(Biff8::VERSION);

$s1 =& $workbook->addWorksheet("S1");
$s2 =& $workbook->addWorksheet("S2");

$f = $workbook->addFormat( array("numFormat"=>"$0.00") );

$s1->write(2,2,"=S2!C3",$f);
$s1->write(3,3,"hello",null);
$s2->write(2,2,"56.78",$f);

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"test.xls\"");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
header("Pragma: public");

$workbook->save('php://output');

?>
MAXakaWIZARD commented 9 years ago

This works as numeric format (built-in):

$format = $this->workbook->addFormat();
$format->setNumFormat(2);
MAXakaWIZARD commented 9 years ago

Your issue is regarding custom formats. Will try to figure out what's wrong.

MAXakaWIZARD commented 9 years ago

@themolecule What version do you use? There is no version parameter in Workbook's constructor in latest version

themolecule commented 9 years ago

it's just left over from the previous version... I'll take that out.

themolecule commented 9 years ago

Frustrating.... Currency format appears to be decimal 65 or 66, despite documentation.

MAXakaWIZARD commented 9 years ago

I will add descriptive constants

MAXakaWIZARD commented 9 years ago

@themolecule I have fixed it. So it is possible to use:

use Xls\NumFormat;

$format->setNumFormat(NumberFormat::TYPE_DECIMAL_2);
//or custom format:
$format->setNumFormat('$0.00');

It is covered by tests. So please check and if it works for you.

themolecule commented 9 years ago

seems to work well! Thanks!