Here, I m using PHPexcel library to reading the file. My excel File look like
在这里,我使用PHPexcel库来读取文件。我的excel文件是这样的
Controller
控制器
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);
}
}
when I am uploading excel file my array like:
当我上传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
)
)
But, I want the array like :
但是,我想要这样的数组:
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
)
)
In above array, my publisher key value is 2. i.e. the Id of publisher into my database. I want this related id publisher name into array. In my database id 2 related publisher name is Cust Angel.
在上面的数组中,我的发布者键值为2。也就是说,发布者的Id进入我的数据库。我想要这个相关的id发布者名称到数组中。在我的数据库id 2相关的出版商名是Cust Angel。
Some genius can help me, Thanks in advance
有些天才可以帮我,谢谢。
Any query related to question please ask me...
任何与问题有关的疑问,请问我……
1 个解决方案
#1
1
Here, My question solution..
在这里,我的问题解决方案。
$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;
}
output:-
输出:
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
)
)
#1
1
Here, My question solution..
在这里,我的问题解决方案。
$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;
}
output:-
输出:
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
)
)