I have cell content looks like "$ 1440" or "€ 970"
But on formula-field (after select cell) value looks like simple number "1440" or "970".
How can I get currency sign ($ or €) by using PhpSpreadsheet?
I tried code
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load('some.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$a = $worksheet->getStyle(10, 3)->getNumberFormat()->getFormatCode();
$b = $worksheet->getCellByColumnAndRow(10, 3)->getValue();
$c = $worksheet->getCellByColumnAndRow(10, 3)->getFormattedValue();
Got:
a - 'General' (string)
b - 1440 (integer)
c -'1440' (string)
Why is format "General" if it "Finance" actually?
You use $reader->setReadDataOnly(TRUE);.
That meant ignore formetting.
Set it to $reader->setReadDataOnly(FALSE);
Related
I want to calculate difference between 2 datetimes in hours with PHPSpreadsheet. This is how Excel does it:
A1 and A2 cells format is:
This is the result on web:
When I change value through PHPSpreadsheet, I get #VALUE! and different value formatting.
$reader = PhpSpreadsheet\IOFactory::createReader("Xlsx");
$target_file = __DIR__ . '/test.xlsx';
$spreadsheet = $reader->load($target_file);
$spreadsheet->getActiveSheet()->setCellValue('A1', '24.6.2020 12:30');
$writer = new PhpSpreadsheet\Writer\Html($spreadsheet);
$output = $writer->generateHTMLHeader();
$output .= $writer->generateStyles(true);
$output .= $writer->generateSheetData();
$output .= $writer->generateHTMLFooter();
$doc = new DOMDocument();
#$doc->loadHTML($output);
echo $doc->saveHTML();
I also tried with formatting like this 6/24/2020 14:30 but the result was same (#VALUE!)
$spreadsheet->getActiveSheet()->setCellValue('A1', '6/24/2020 14:30');
Anyone got any idea on how this should be done?
In an Excel document, dates are stored as numbers, not strings. So you need to pass the correct number to setCellValue().
PhpSpreadsheet provides the utility method Date::stringToExcel() to convert strings to Excel dates. You can use it like this:
$date = PhpSpreadsheet\Shared\Date::stringToExcel('2020-06-24 12:30');
$spreadsheet->getActiveSheet()->setCellValue('A1', $date);
This question already has answers here:
PHPExcel how to get column index from cell
(3 answers)
Closed 2 years ago.
I am using https://phpspreadsheet.readthedocs.io/en/latest/ phpspreadsheet to import files to database.I need to get the total number of columns in the uploaded excel before importing it to database.
I found getHighestDataColumn() which returns the highest coulmn as alphabets.
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fileName);
$worksheet = $spreadsheet->setActiveSheetIndex(0);
$high=$spreadsheet->setActiveSheetIndex(0)->getHighestColumn();
dump($high);
die;
which gives output as I
Is there any way to get it as numbers ..??
If you only have A-Z you can convert the chars to ascii and get their int value using ord.
// Get ascii offset for alphabet
$start = ord("A");
$current = "I";
// Calculate char position from offset A
echo ord($current) - $start;
Found the solution as
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fileName);
$worksheet = $spreadsheet->setActiveSheetIndex(0);
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
dump($highestColumnIndex);
die;
This will output the index of highestColumn ..
I'm using this piece of code to format numbers from my database so it becomes currency
$formnum = numfmt_create( 'fr_FR', NumberFormatter::CURRENCY);
$datavalue = !empty($datavalue) ?
numfmt_format_currency($formnum, intval($datavalue), 'EUR') : '';
but if $datavalue is equal to (let say) 10, I'll get 10,00 €
But obviously what I wanted is 10 €
(if you want to try it)
$formnum = numfmt_create( 'fr_FR', NumberFormatter::CURRENCY);
$datavalue = numfmt_format_currency($formnum,intval("10"), 'EUR');
echo $datavalue;
Try something like this, using the full scope of the NumberFormatter class (namely, the FRACTION_DIGITS attribute):
$fmt = new NumberFormatter('fr_FR', NumberFormatter::CURRENCY);
$fmt->setTextAttribute(NumberFormatter::CURRENCY_CODE, 'EUR');
$fmt->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);
echo $fmt->formatCurrency($datavalue, 'EUR');
Docs are here: http://php.net/manual/en/numberformatter.formatcurrency.php
I've found that PHPExcel can read vlookup value if it starts with an alphabet, but when it comes to numbers, the value returns #N/A.
How can I make the PHPExcel read numbers value vlookup?
I tried changing the cell format of the vlookup table range to text, but that doesn't do anything.
Here's the code:
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $this->excel = $objReader->load($file_path);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet); // Here get total count of row in that Excel sheet
for($z=2;$z<=$arrayCount;$z++)
{
$a = $allDataInSheet[$z]["A"];
$b = $allDataInSheet[$z]["B"];
$c = $allDataInSheet[$z]["C"];
echo "a: ".$a."<br/>";
echo "b: ".$b."<br/>";
echo "c: ".$c."<br/>";
}
the result:
a: #N/A
b: Orange Juice
c: Z02
the excel:
Code Description Item
------------ -------------- -----------
3000000073 Orange Juice Z02
where column code and item are a vlookup formula, while description is an input text.
I'm loading an Excel file that has cells with time data, e.g. 08:00:00. But when I try to read those cells with getValue(), it returns some floating point numbers instead of the actual time (in case of 08:00:00, it returns 0.3333333). Here's my code:
$objPHPExcel = PHPExcel_IOFactory::load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
echo $objWorksheet->getCellByColumnAndRow(3, 5)->getValue();
How do I bypass this weird conversion?
PHPExcel 1.7.6 and Excel 2003 Worksheet (.xls)
You need to apply cell format for this:
$cell = $objWorksheet->getCellByColumnAndRow(3, 5);
$cell_value = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'hh:mm:ss');
echo $cell_value;