Zero in first of my cell data will remove in PHPExcel - php

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');

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('#');

Copying excel column and inserting into new array

Problem:
What I am trying to achieve is to copy a selected range of cells from one excel spreadsheet and insert it into a newly generated one using laravel-excel and phpspreadsheet libraries. So far, the code that I've got, sort of does that but not idealy.
Excel::load($file, function($reader)
{
$activeSheet = $reader->getActiveSheet();
$this->data = $activeSheet->rangeToArray(
'A1:A27', // The worksheet range that we want to retrieve
NULL, // Value that should be returned for empty cells
true, // Should formulas be calculated (the equivalent of getCalculatedValue() for each cell)
true, // Should values be formatted (the equivalent of getFormattedValue() for each cell)
true // Should the array be indexed by cell row and cell column
);
});
// Create new file.
$newExport = Excel::create('Filename', function($excel) {
$excel->sheet('Sheetname', function($sheet) {
$sheet->fromArray($this->data, null, 'B1', true);
});
});
// Export newly created file.
$newExport->export('xlsx');
The problem is that it also inserts column name into a first cell (0 on screenshot, as I had indexing turned off, with indexing on, it would insert A) , as you can see on a screenshot bellow.
Actual result:
Expected result:
Stuff I have tried:
Tried to turn off indexing inside rangeToArray method.
Add column name into ignore list (2nd parameted in fromArray()), but that's not pragmatic, as I would end up adding every single column name into ignore list, moreover, it inserts blank value into first column and starts from B2 cell.
If anyone could give me some ideas how to resolve this case, that would be great.
Thanks!
The answer to the case is following...
Read the documentation for the library that you're actually using, rather than the one it's based on.
The reason why it was adding column headings, was due to laravel-excel library fromArray method default property values, which has heading generation enabled by default.
$sheet->fromArray($this->data, null, 'B1', true);
change to
$sheet->fromArray($this->data, null, 'B1', true, false);
Accepted parameters:
fromArray($source, $nullValue, $startCell, $strictNullComparison, $headingGeneration).

PHPOffice/PHPExcel PHPExcel_Style_NumberFormat FORMAT_CURRENCY_USD_SIMPLE correct way to use this.

I am trying to use FORMAT_CURRENCY_USD_SIMPLE .
I am able to use dates, integer and general succefully. But i am having problem with this.
My Cell content can be like this.
Here you can see, I am using "$75.00" is my cell content. I am putting $ as my cell content as I get this data from my query.
Is CONTENT of CELL value we put in setCellValueByColumnAndRow should be without "$" or with "$". I have tried it with "$".
DOCUMENTS
I have not tried without "$".
So what will be the correct CONTENT and what will be the correct format code.
I have used "FORMAT_CURRENCY_USD_SIMPLE" , "FORMAT_CURRENCY_USD" , '"$"#,##0.00_-' ( directly ) , '$#,##0_-' ( right now ).
My all currencny number will be like $1,356.25. If you follow this structure.
Which format code should i use for content value like this.
My code , It works with date and numbers.
$areaOfExcel = $column_alphabet.$row_start_data.":".$column_alphabet.$excel_current_row_index ;
$this->excel_active_sheet->getStyle( $areaOfExcel )
->getNumberFormat()
->setFormatCode( $dataTypeFromAdoDb );
My main concern is what should be correct format code or what should be correct content. to use $ sign and format them properly.
The content value in the cell should be a simple floating point number, with no currency code, no thousands separator, etc..... exactly as it should be in MS Excel itself if you want to use a currency format number mask.
$value = 1234.56; // float value
$objPHPExcel->getActiveSheet()
->setCellValue('A1', $value);
$objPHPExcel->getActiveSheet()
->getStyle('A1')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);

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