PHPExcel taking format of empty cells - php

In an excel sheet where I previousely formatted the whole first colum as a date, I changed afterwards the format of the used cells to General (cells A1 to A11). So from cell A12, this first column still has the previous date format.
When I try to use the values of the first column (A1, A2, ..., A11) it identifies their format as a date, and gives a wrong result.
For example, this retruns a date format (d/mm/yyyy), which is not the case (see picture)
$sheet->getStyle('A3')->getNumberFormat()->getFormatCode();
Also the whole column has this format :
$sheet->getStyle('A')->getNumberFormat()->getFormatCode();
Is there a way to make sure the format of the cell itself is considered?
(when I indicated the format of this cells explicitely, the right format was used)
After some investigation, found that this problem occurs only for the .xlsx files (using reader PHPExcel_Reader_Excel2007) and not for the .xls files (reader PHPExcel_Reader_Excel5).

You can set the whole row/col format:
$sheet->getStyle('A')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
Because you have changed only the format of A1 to A11, the rest are still with the old format. So setting format of the Whole Column/Row will fix this problem

After some more invetigation found a possible cause in reader Excel2007.php.
There is a problem with the condition When setting the style (line 933):
if ($c["s"] && !$this->_readDataOnly) {
$cell->setXfIndex(isset($styles[intval($c["s"])]) ? intval($c["s"]) : 0);
}
In case of General cell format, $c["s"] is not defined, and the _xfIndex of the cell is not set (not passing the if statement). This gives (I didn't found why) a problem with the stored _xfIndex of the cell.
When removing the $c["s"] condition, the right style is set.
if (/*$c["s"] &&*/ !$this->_readDataOnly) {
$cell->setXfIndex(isset($styles[intval($c["s"])]) ? intval($c["s"]) : 0);
}
Hoping there are no hidden consequences.

Related

Tables ignore given format

I'm trying to add some tables to a document, which after lot of work, it's showing right. Generated with PHPWord. The issue is that whatever I add in the table,row or cell style, it doesn't modify the table.
I've already tried: $table->addRow(15), $table->addRow(array('height'=>15)) and as is shown in the code below.
Also, there is no documentation on how to add a cell with two lines of text, because below 'Sign' I have to add 'Name'.
//TABLE1
$table = $section->addTable(array('width'=>100, 'borderSize'=> 1, 'borderColor'=>'000000'));
$table->addRow(15,array('height'=>15));
$table->addCell(array('width'=>150))->addText('COMPANY', $smallBoldFont);
$table->addRow();
$table->addCell()->addText('');
$table->addRow();
$table->addCell()->addText('');
//TABLE1
$section->addTextBreak(2);
//TABLE2
$table2 = $section->addTable(array('borderSize'=> 1, 'borderColor'=>'000000'));
$table2->addRow(100,array('height'=>80));
$table2->addCell(150,array('width'=>800))->addText('Recibed', $smallFont8);
$table2->addRow();
$table2->addCell()->addText('Time:', $smallFont8);
$table2->addCell()->addText('Sign:', $smallFont8);
//TABLE2
Is there a reason why PHPWord ignores my format style to tables? Is there any other way of doing it better?
Desired output:
Actual output:
In documentation is all. Just to read it deeply. Here is section how to create tables, including creation of tables with multicolumn cells
I don't know if PHPWord has some pre-defined units for case if you miss to use them. But it seems you need to set units for width. But you should test units and numbers as you need.
Order to create multicolumn cell is gridSpan(). So line for creation of the first row of the second table (probably) should be:
$table2->addCell(150,array('width'=>800))->gridSpan(2) ->addText('Recibed', $smallFont8);
PHPWord uses twip as default unit. And all cells have to match its width.
The smallest cell is taken for entire column.
For convertion from twips to other units may be used EndMemo (Topography Conversion Online).
250 pixels are 3750 twips.

How to put 0001 in excel report using PHPExcel

I'm having some problems with my code in PHPexcel on generating report. I've got numbers like 0001,0002,0087 in a textbox and when I transfer it to Excel, to make a report the output is 1,2,87. Why is that?
my var to pass value to generate excel report using PHPExcel
$aic = isset($_POST['aic'.$n]) ? $_POST['aic'.$n] : "";
I think setting your cell format to 4 digits number should do the trick. Try this:
$objPHPExcel->getActiveSheet()->getStyle('A1')->getNumberFormat()->setFormatCode('0000');
The answer to your problem is given in the documentation ;)
https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/07-Accessing-Cells.md#setting-a-number-with-leading-zeroes
There are 2 ways of achieving that:
Set the data type as string
// Set cell A8 with a numeric value, but tell PHPExcel it should be treated as a string
$objPHPExcel->getActiveSheet()->setCellValueExplicit(
'A8',
"01513789642",
PHPExcel_Cell_DataType::TYPE_STRING
);
or set a specific number format:
// Set cell A9 with a numeric value
$objPHPExcel->getActiveSheet()->setCellValue('A9', 1513789642);
// Set a number format mask to display the value as 11 digits with leading zeroes
$objPHPExcel->getActiveSheet()->getStyle('A9')
->getNumberFormat()
->setFormatCode(
'00000000000'
);

Zero in first of my cell data will remove in PHPExcel

I am trying to export my data to excel with PHPExcel,
I have a column which many of data on this column will start with 0 for example 0054672351 or 057524572 .
Now when I am trying to export to excel, my data will change to 54672351 or 57524572.
I am trying to change Data format of this column to text but I have this problem yet !
What should I do ?
For the second time of answering this same question here today:
Either:
// Set the value explicitly as a string
$objPHPExcel->getActiveSheet()
->setCellValueExplicit(
'A1',
'0054672351',
PHPExcel_Cell_DataType::TYPE_STRING
);
or
// Set the value as a number formatted with leading zeroes
$objPHPExcel->getActiveSheet()
->setCellValue('A3', 54672351);
$objPHPExcel->getActiveSheet()
->getStyle('A3')
->getNumberFormat()
->setFormatCode('0000000000');
Note that in the first case I'm calling the setCellValueExplicit() method, not the setCellValue() method.
If you're populating blocks of data in one step using the fromArray() method, then the latter approach is probably easier, especially as you can set the style for a whole block of cells in one step once you've populated all the data.
$objPHPExcel->getActiveSheet()
->getStyle('A3:A123')
->getNumberFormat()
->setFormatCode('0000000000');

Set data type in excel cells in PHP Excel class

Using PHP Excel class , how can I set percentage data type in Excel cells.
I tried with following code:
$objPHPExcel->getActiveSheet()->getStyle('I2')
->getNumberFormat()->applyFromArray(
array(
'code' => PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00
)
);
But in this case if I add value to 'I2' as '2.35', it displayed as '235.00%' in generated excel file.
Is there anyway to show it correctly?
This is MS Excel behaviour:
Open MS Excel, set a cell value to 2.35, then set the cell number format to percentage (with or without decimals) and you'll see 235.00% as the formatted value.
PHPExcel simply reflects this.
If you want to display 2.35% in Excel, then you need the cell value as 0.0235 and then set the number format mask to percentage. Do the same in PHPExcel, and you'll get the result you want
Note that this does not change the cell value in any way, simply the way that it is displayed.
Alternatively, set the number format mask to:
#,##0.00"%"

PHP MySQL Excel to xls?

This my example code:
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A2', '1.0');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', '0.0');
$objPHPExcel->getActiveSheet()->SetCellValue('C2', '1.2');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', '100');
I am getting in excel sheet is round off values for A2->1 and B2->0, What I need to output in excel sheet is A2->1.0 and B2->0.0. I need the float values end with .zero (.0) to print.
Please help me out...
You need to set the display format of the cells to Number. The default in Excel is General, which will display the values as you describe. I don't know if the PHP Excel interface will let you set the cell format, but that's where you should start.
EDIT: According to the PHPExcel website it supports setting cell formats. Read the docs to find out how to set the appropriate format.
Export / Import excel to mysql.
DEAME3P
I think u want something like this
$objPHPExcel->getActiveSheet()->getStyle('D1')->getNumberFormat()->setFormatCode('###.00');
I use it on my project, any doubts, ask away

Categories