PHPOffice / PHPExcel

ARCHIVED
Other
11.46k stars 4.19k forks source link

How do I can get decimal data into array as length as length excel decimal data? #1220

Closed kevingatp closed 7 years ago

kevingatp commented 7 years ago

Halo, I've using PHPExcel for getting data from uploaded Excel file. But I've an issue according to reading data from Excel as an array result, the file contain an data in decimal type (with 1 decimal place), but the PHPExcel read the data as a decimal with more than one decimal places.

Example: This is my Excel file screenshot_13

and this is the result screenshot_12

I need the data in the array has decimal length same with the excel decimal length, because I wanna use it for sum.

This is my code: ` $objPHPExcel = new \PHPExcel();

        $fileName = Yii::getAlias('@webroot/trash/out/') . $name;
        $inputFiles = fopen(Yii::getAlias('@webroot/trash/out/') . $name, "r");
        try {
            $inputFileType = \PHPExcel_IOFactory::identify($fileName);
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($fileName);
        } catch (Exception $ex) {
            die('Error');
        }
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestDataRow();
        $highestColumn = $sheet->getHighestDataColumn();
        $colNumber = PHPExcel_Cell::columnIndexFromString($highestColumn);
        $col = $colNumber - 1;
        $arrayData = [];
        for ($row = 1; $row <= $highestRow; ++$row) {
            $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, $formulaValue, NULL);
            if (!is_null($rowData[0][$col])) {
                $arrayData[] = array_map(function($values) {
                    $tempArrayKey = [];
                    foreach ($values as $key => $value) {
                        $newKey = $key + 1;
                        $tempArrayKey[] = $newKey . '_' . $value;
                    }
                    return $tempArrayKey;
                }, $rowData);
            }
        }`

How do I can get decimal data from Excel with same decimal length?

Thanks.