Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue on .csv files on PHPExcel

Tags:

php

csv

phpexcel

I have a problem regarding PHPExcel when reading .csv files.

I wanted to get the values from the .csv file, but the problem is the data from a specific row considered as a single cell.

heres my code:

        include 'Classes/PHPExcel/IOFactory.php';
        $inputFileType = 'CSV';
        $inputFileName = $_FILES['file']['tmp_name'];
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileName);

        $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

        $table = "<table border=1><tr><td>first</td><td>middle</td><td>last</td><td>email</td>";

        for ($x = 2; $x <= count($sheetData); $x++){
            foreach ($sheetData[$x] as $data){
                $first = $sheetData[$x]['A'];
                $middle = $sheetData[$x]['B'];
                $last = $sheetData[$x]['C'];
                $email = $sheetData[$x]['D'];   
            }

            $table .= "<tr><td>" . $first ."</td><td>" . $middle . "</td><td>" . $last . "</td><td>" . $email . "</td></tr>";

        }

        $table .= "</table>";

        echo $table;

It is working on .xls and .xlsx files and I get the desired output that I wanted.

like image 975
rhy Avatar asked Dec 15 '22 06:12

rhy


1 Answers

This works fine:

$objReader->setDelimiter("\t"); 

However, when you are not 100% sure if its tab or comma separated there seems to be no way to add BOTH e.g. $objReader->setDelimiter("\t",); which is something that would be required. When you open Excel and go to Import CSV the actual on screen steps allow you to specify multiple delimiters which is something that would be cool.

Is this something you are working on with PHP Office?

On a separate note here are two links that help you using PHP to find out if the file is comma, tab, or pipe separated - quiet a clever solution:

how to find out if csv file fields are tab delimited or comma delimited

How should I detect which delimiter is used in a text file?

like image 83
MartinD Avatar answered Dec 30 '22 16:12

MartinD