PHP MySQL Excel to xls? - php

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

Related

number cell format with text

I want to export my data to excel using phpspreadsheet but my data is number in 12 character. So I need to display all the character (121212121212) instead of (1.21212E+11).
I have try the format using
PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT
and
PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER
It doesn't work.
This is my cell formatting code:
$spreadsheet->getActiveSheet()->getStyle('B')->getNumberFormat()
->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
This is my value insert code:
$sheet->setCellValue('A'.$i, $i-1);
$sheet->setCellValue('B'.$i, $useric);
If I use the FORMAT_TEXT the result is this:
![result](https://i.imgur.com/6xxKDkv.png)
when using PHPSpreadsheet, this is what I do.
$spreadsheet->getActiveSheet()
->getCell('A1')
->setValueExplicit(
$someNumber,
\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING2
);
If by any chance you using PHPExcel, maybe you can do something like this
$spreadsheet->getActiveSheet()
->setCellValueExplicit('A1', $someNumber, PHPExcel_Cell_DataType::TYPE_STRING);
You can set the formatcode to # (strings)
$spreadsheet->getActiveSheet()->getStyle('B')->getNumberFormat()
->setFormatCode('#');
This works, in all the sheet
$spreadsheet ->getDefaultStyle()->getNumberFormat()->setFormatCode('#');

Set different font-color inside 1 cell using PHPExcel

I want to make the text have different color inside 1 cell using PHPExcel, but i can't find the info about it. Is it possible?
I believe it can be done from the xls, but can it be done using programming, in PHPExcel?
Yes it can be done in PHPExcel, using Rich Text objects.
They are described in the PHPExcel documentation and there are examples provided
$objRichText = new PHPExcel_RichText();
$objRichText->createText('This invoice is ');
$objPayable = $objRichText->createTextRun('payable within thirty days after the end of the month');
$objPayable->getFont()->setBold(true);
$objPayable->getFont()->setItalic(true);
$objPayable->getFont()->setColor(
new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN )
);
$objRichText->createText(', unless specified otherwise on the invoice.');
$objPHPExcel->getActiveSheet()->getCell('A18')->setValue($objRichText);

PHPExcel: how to read values from cells which the contents come from cells in other sheets

I have spent the last days trying to solve this issue. I have a spreadsheet with a couple tabs (sheets). The last tab has the values input in all other tabs. Those values are populated automatically by using formulas like:
=$'otherSheetName'.A1
If I leave calculateFormulas = true those values are read as null.
If I set calculateFormulas = false the values are read as the literal formula.
I was firstly using LaravelExcel but I gave up from finding a solution for that, so I moved to PHPExcel. Solutions for Laravel Excel will be appreciated too though.
Thanks
Update:
How I am reading values in PHPExcel:
$inputFileType = PHPExcel_IOFactory::identify($file->getRealPath());
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setLoadSheetsOnly("MySheet");
$objPHPExcel = $objReader->load($file->getRealPath());
$rows = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
Then, I iterate over the array $rows and extract the values that I want.
You need to load all the dependency sheets that are used in those formulae, though you don't necessarily need to load every worksheet, but can pass a list of sheet names to load as an array
$objReader->setLoadSheetsOnly(["MySheet", "OtherSheet1", "OtherSheet2"]);
You don't need to iterate over any other worksheets, but can set the active worksheet to whichever sheet you want
$objReader->setActiveSheetIndexByName("MySheet");
and then any calls to the active sheet ($objPHPExcel->getActiveSheet()->...) to access cells will be a reference to that worksheet.

PHPExcel taking format of empty cells

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.

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"%"

Categories