简体   繁体   English

在PHPExcel上发布.csv文件

[英]Issue on .csv files on PHPExcel

I have a problem regarding PHPExcel when reading .csv files. 我在读取.csv文件时遇到PHPExcel问题。

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. 我想从.csv文件中获取值,但问题是来自特定行的数据被视为单个单元格。

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. 它正在处理.xls.xlsx文件,我得到了我想要的所需输出。

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 eg $objReader->setDelimiter("\\t",); 但是,当你不是100%确定它的标签或逗号是否分开时,似乎无法添加两个例如$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. 当您打开Excel并转到导入CSV时,实际的屏幕上的步骤允许您指定多个分隔符,这将是很酷的。

Is this something you are working on with PHP Office? 这是你正在使用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: 另外,这里有两个链接,可帮助您使用PHP查找文件是否以逗号,制表符或管道分隔 - 安静一个聪明的解决方案:

how to find out if csv file fields are tab delimited or comma delimited 如何找出csv文件字段是否以制表符分隔或以逗号分隔

How should I detect which delimiter is used in a text file? 我该如何检测文本文件中使用的分隔符?

This worked for me: 这对我有用:

        $objReader = new PHPExcel_Reader_CSV();
        $objReader->setDelimiter(';');
        $objReader->setEnclosure('');
        $objReader->setLineEnding("\r\n");
        $objReader->setSheetIndex(0);
        $objPHPExcel = $objReader->load($myFile);

More info https://docs.typo3.org/typo3cms/extensions/phpexcel_library/1.7.4/manual.html#_Toc237519888 更多信息https://docs.typo3.org/typo3cms/extensions/phpexcel_library/1.7.4/manual.html#_Toc237519888

So what is the separator in your file? 那么文件中的分隔符是什么? Is it a comma, a semi-colon, a tab, something else? 它是逗号,分号,标签还是别的什么?

PHPExcel doesn't yet have an automagic detect mode, so unless you specify what separators and enclosures to use, it will default to a comma separator, and a double quote (") enclosure. If your file is using tabs, or semi colons, or some other character as a separator instead, then you need to manually tell the CSV reader what character to use, otherwise it will treat the row as a single cell. PHPExcel还没有自动检测模式,因此除非您指定要使用的分隔符和附件,否则它将默认为逗号分隔符和双引号(“)附件。如果您的文件使用制表符或半冒号,或者将某个其他字符作为分隔符,然后您需要手动告诉CSV读取器使用哪个字符,否则它会将该行视为单个单元格。

There's a whole section of the User documentation for the Readers devoted to explaining these options for CSV files (section 4.6). 阅读器的用户文档的一部分专门用于解释CSV文件的这些选项(见第4.6节)。

Note that I'm targeting logic to "best guess" separator and enclosure values from the file itself at the #phpnw13 hackathon, but until then you need to specify manually if it isn't the defaults 请注意,我的目标逻辑是从#phpnw13黑客马拉松中的文件本身“最佳猜测”分隔符和机箱值,但在此之前,如果不是默认值,则需要手动指定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM