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'
);
Related
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('#');
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);
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');
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"%"
I have a cell and I need to add the sign "%" at the end of it, just for display. That cell is linked to other cells that have a formula that uses its value.
I have tried adding the "%" in the cell and using ABS in the formula. e.g. 100 - ABS(H23)This would have worked if after I use concat in php, the output would not have added an extra single column. ...->setCellValue('H23', $number.'%');
Does this to the cell '33%
1.. Any other solution for my title question?
2.. How do I make the output excel not have the single quote when I concatenate strings to it?
Thanks plenty!
If this is just for display, then set the cell value correctly, and give the cell a numberFormat mask as you would in MS Excel itself.
$objPHPExcel->getActiveSheet()->setCellValue('A5', 0.33);
$objPHPExcel->getActiveSheet()->getStyle('A5')
->getNumberFormat()->setFormatCode(
PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE
);
EDIT
To avoid MS Excel automatically multiplying by 100 when using a percent-based format mask: using a string format mask:
'0 "%"'
$objPHPExcel->getActiveSheet()->setCellValue('A5', 0.33);
$objPHPExcel->getActiveSheet()->getStyle('A5')
->getNumberFormat()->setFormatCode(
'0 "%"'
);