如何使用codeigniter读取关联数组中的excel文件?

问题描述 投票:1回答:1

在这里,我使用PHPexcel库来读取文件。我的excel文件看起来像

enter image description here

调节器

class Home extends CI_Controller {
    public function index() {
        $this->load->library('excel');
        $reader = PHPExcel_IOFactory::createReader('Excel2007');
        $reader->setReadDataOnly(true);
        $file = isset($_FILES["uploadexcel"]['tmp_name']) ? $_FILES["uploadexcel"]['tmp_name'] : '';
        $excel = $reader->load($file);
        foreach ($excel->getWorksheetIterator() as $worksheet) {
            $worksheets = $worksheet->toArray();
        }
        echo '<pre>';print_r($worksheets);
    }
}

当我上传excel文件我的数组如:

 Array
 (
  [0] => Array
    (
        [0] => url
        [1] => category_1
        [2] => category_2
        [3] => language
        [4] => f_n
        [5] => publishers
        [6] => cost
        [7] => cost_currency
        [8] => processing_time
        [9] => example
        [10] => note
    )
    [1] => Array
    (
        [0] => gettingaway.com
        [1] => 10
        [2] => 14
        [3] => GA
        [4] => nofollow
        [5] => 2
        [6] => 1245
        [7] => 
        [8] => 12
        [9] => testing
        [10] => Testing Value
    )


 )

但是,我希望数组如下:

Array
   (
    [0] => Array
    (
        [url] => gettingaway.com
        [category_1] => 10
        [category_2] => 14
        [language] => GA
        [f_n] => nofollow
        [publishers] => Cust Angel
        [cost] => 12
        [cost_currency] => USD
        [processing_time] => 12
        [example] => example
        [note] => note
    )   
    [1] => Array
    (
        [url] => thebusbench.com
        [category_1] => 5
        [category_2] => 13
        [language] => GA
        [f_n] => nofollow
        [publishers] => Cust Angel
        [cost] => 12
        [cost_currency] => USD
        [processing_time] => 6
        [example] => example
        [note] => note
    )   
    [2] => Array
    (
        [url] => travelintelligence.net
        [category_1] => 6
        [category_2] => 11
        [language] => GA
        [f_n] => nofollow
        [publishers] => Cust Angel
        [cost] => 12
        [cost_currency] => USD
        [processing_time] => 2
        [example] => example
        [note] => note
    )      
 )

在上面的数组中,我的发布者键值是2.即发布者进入我的数据库的Id。我希望这个相关的id发布者名称成为数组。在我的数据库中,id 2相关的发布者名称是Cust Angel。

php arrays excel codeigniter phpexcel
1个回答
1
投票

在这里,我的问题解答..

 $reader = PHPExcel_IOFactory::createReader('Excel2007');
    $reader->setReadDataOnly(true);
    $file = isset($_FILES["uploadexcel"]['tmp_name']) ? $_FILES["uploadexcel"]['tmp_name'] : '';
    $objPHPExcel = $reader->load($file);
    $objWorksheet = $objPHPExcel->getActiveSheet();
    $header=true;
    if ($header) {
        $highestRow = $objWorksheet->getHighestRow();
        $highestColumn = $objWorksheet->getHighestColumn();
        $headingsArray = $objWorksheet->rangeToArray('A1:' . $highestColumn . '1', null, true, true, true);
        $headingsArray = $headingsArray[1];
        $r = -1;
        $namedDataArray = array();
        for ($row = 2; $row <= $highestRow; ++$row) {
            $dataRow = $objWorksheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, null, true, true, true);
            if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
                ++$r;
                foreach ($headingsArray as $columnKey => $columnHeading) {
                    $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
                }
            }
        }
    } else {
        //excel sheet with no header
        $namedDataArray = $objWorksheet->toArray(null, true, true, true);
    }
    echo '<pre>';print_r($namedDataArray);exit;
 }

输出: -

Array
 (
  [0] => Array
         (
        [url] => gettingaway.com
        [category_1] => Finance
        [category_2] => General
        [language] => GA
        [f_n] => nofollow
        [publishers] => KAlw
        [cost] => 1245
        [cost_currency] => 
        [processing_time] => 12
        [example] => testing
        [note] => Testing Value
    )

[1] => Array
    (
        [url] => thebusbench.com
        [category_1] => Books & Writing
        [category_2] => Games
        [language] => GA
        [f_n] => nofollow
        [publishers] => Miclede
        [cost] => 12
        [cost_currency] => USD
        [processing_time] => 11
        [example] => fsdfsd fsdfd
        [note] => asd
    )

[2] => Array
    (
        [url] => travelintelligence.net
        [category_1] => Finance
        [category_2] => Food & Drinks
        [language] => GA
        [f_n] => follow
        [publishers] => Micel 
        [cost] => 123
        [cost_currency] => 
        [processing_time] => 25
        [example] => fsdfsd fsdfd
        [note] => dfmlsdflsdk fjsdlfj sdlfjsdl fjld
    )

[3] => Array
    (
        [url] => littleyayas.com
        [category_1] => Automotive
        [category_2] => General
        [language] => GA
        [f_n] => follow
        [publishers] => Cust Poo
        [cost] => 10200
        [cost_currency] => 
        [processing_time] => 5
        [example] => asds
        [note] => adasdd
    )

[4] => Array
    (
        [url] => tripwheeling.com
        [category_1] => Finance
        [category_2] => Finance
        [language] => GA
        [f_n] => follow
        [publishers] => Cust Angel
        [cost] => 152000
        [cost_currency] => 
        [processing_time] => 13
        [example] => dasd
        [note] => adas
    )

[5] => Array
    (
        [url] => gettingaway.com
        [category_1] => Home & Garden
        [category_2] => General
        [language] => EN
        [f_n] => nofollow
        [publishers] => abc
        [cost] => 250
        [cost_currency] => USD
        [processing_time] => 10
        [example] => asdasd
        [note] => asd
    )

 )
© www.soinside.com 2019 - 2024. All rights reserved.