php - CodeIgniter 的 CSV 导入库

标签 php csv codeigniter-2 helper xls

<分区>

需要将 csv 或 xls 导入到使用 CodeIgniter 创建的应用程序中。有这方面的图书馆吗?任何建议表示赞赏。

最佳答案

这里有一个简单的方法来做到这一点。我不知道人们在做什么,但我用这个

这是我的csv阅读器库

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CSVReader {

    var $fields;            /** columns names retrieved after parsing */ 
    var $separator = ';';    /** separator used to explode each line */
    var $enclosure = '"';    /** enclosure used to decorate each field */

    var $max_row_size = 4096;    /** maximum row size to be used for decoding */

    function parse_file($p_Filepath) {

        $file = fopen($p_Filepath, 'r');
        $this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
        $keys_values = explode(',',$this->fields[0]);

        $content    =   array();
        $keys   =   $this->escape_string($keys_values);

        $i  =   1;
        while( ($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false ) {            
            if( $row != null ) { // skip empty lines
                $values =   explode(',',$row[0]);
                if(count($keys) == count($values)){
                    $arr    =   array();
                    $new_values =   array();
                    $new_values =   $this->escape_string($values);
                    for($j=0;$j<count($keys);$j++){
                        if($keys[$j] != ""){
                            $arr[$keys[$j]] =   $new_values[$j];
                        }
                    }

                    $content[$i]=   $arr;
                    $i++;
                }
            }
        }
        fclose($file);
        return $content;
    }

    function escape_string($data){
        $result =   array();
        foreach($data as $row){
            $result[]   =   str_replace('"', '',$row);
        }
        return $result;
    }   
}
?> 

和 Controller 方法

function readExcel()
{
        $this->load->library('csvreader');
        $result =   $this->csvreader->parse_file('Test.csv');

        $data['csvData'] =  $result;
        $this->load->view('view_csv', $data);  
}

这是 View

<table cellpadding="0" cellspacing="0" width="100%">
    <tr>
            <td width = "10%">ID</td>
            <td width = "20%">NAME</td>
            <td width = "20%">SHORT DESCRIPTION</td>
            <td width = "30%">LONG DESCRIPTION</td>
            <td width = "10%">STATUS</td>
            <td width = "10%">PARENTID</td>
    </tr>

            <?php foreach($csvData as $field){?>
                <tr>
                    <td><?php echo $field['id']?></td>
                    <td><?php echo $field['name']?></td>
                    <td><?php echo $field['shortdesc']?></td>
                    <td><?php echo $field['longdesc']?></td>
                    <td><?php echo $field['status']?></td>
                    <td><?php echo $field['parentid']?></td>
                </tr>
            <?php }?>
</table>

引用 Here

关于php - CodeIgniter 的 CSV 导入库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15446796/

相关文章:

php - 从一个数组中减去另一个数组

php - 德鲁巴 7 : Get user information on account creation

php - 回显 '<pre>' 以保留中断与中断字冲突

C# - 按列拆分 CSV 文件

php - 如何在 Codeigniter 中将变量从一个 Controller 传递到另一个 Controller

php - 多个域的单一 Codeigniter 代码库

php - 如何统计静态页面的浏览量?

read.csv 在 R 中警告 'EOF within quoted string' 但在 EXCEL 中成功读取

php - 我可以将 Ion auth 设置为通过用户名或电子邮件登录吗

在 CSV 中跨列写入的 Python 循环