PHP函数,需要输入一个excel文件的名称,并返回关联数组的数组与Excel文件的内容 [英] php function that takes as input the name of a excel file, and returns an array of associative arrays with the content of excel file

查看:145
本文介绍了PHP函数,需要输入一个excel文件的名称,并返回关联数组的数组与Excel文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数,需要输入一个excel文件的名称,并返回关联数组的数组与Excel文件的内容。

I need a function that takes as input the name of a excel file, and returns an array of associative arrays with the content of excel file.

这样的funtion的签名

signature of the funtion like this

function getExcelData($excelfilepath) { 
    return $exceldata; 
} 

在我的情况下,每个元素是Excel文件的行(第一行包含列名

In my case each element is a row of the excel file (the first line contains the name of the column

$exceldata[0] it's a row 2 
$exceldata[1] è la row 3 
... 
$exceldata[n] it's a row n-2 

所以在我的情况下,数组有3个元素(第一个是列名)。
每一行,然后每个数组元素,依次行包含数据的关联数组:

So in my case the array has 3 elements (the first is the name of the columns). Each row, then each array element, is in turn an associative array that contains the row data:

$exceldata[0]["campo1"] contains 1 and $exceldata[2]["pippo"] contains 3

做到这一点的我尝试使用phpexcel和我写这样的功能。

for do this i try use phpexcel and i write function like this

$inputFileName=$UploadDirectory . $FileName;
function getExcelData($inputFileName){
    $inputFileName=$UploadDirectory . $FileName;
    try {
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $phpExcel = $objReader->load($inputFileName);
        $sheet = $phpExcel->getSheet(0);
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    $highestrow = $sheet->getHighestRow();
    $highestcolumn = $sheet->getHighestColumn();
    $columncount = PHPExcel_Cell::columnIndexFromString($highestcolumn);
    $titles = $sheet->rangeToArray('A1:' . $highestcolumn . "1");
    $body = $sheet->rangeToArray('A2:' . $highestcolumn . $highestrow);
    $table = array();
    for ($row = 0; $row <= $highestrow - 2; $row++) {
        $a = array();
        for ($column = 0; $column <= $columncount - 1; $column++) {
            $a[$titles[0][$column]] = $body[$row][$column];
        }
        $table[$row] = $a;
    }
    var_dump ($table);
    return $table;
}

但不工作...请我需要帮助!

but not work... please i need help!

推荐答案

最明显的一个明显的问题:

The obvious obvious problem:

function getExcelData($inputFileName){
    $inputFileName=$UploadDirectory . $FileName;

您正在传递中的 $查找inputfilename 值,而是覆盖与 $ UploadDirectory。 $文件名,这两者都不存在变量在函数的范围;但只要你做了正确的值传递为 $查找inputfilename ,则函数应该没有问题工作

You're passing in the $inputFileName value but overwriting that with $UploadDirectory . $FileName, neither of which variables exists in the scope of your function; but as long as you do pass in the correct value for $inputFileName, then the function should work without problem

您可以简化虽然它:

function getExcelData($inputFileName){
    try {
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $phpExcel = $objReader->load($inputFileName);
        $sheet = $phpExcel->getSheet(0);
    } catch (Exception $e) {
        echo $e->getMessage();
        die();
    }
    $highestrow = $sheet->getHighestRow();
    $highestcolumn = $sheet->getHighestColumn();
    $titles = $sheet->rangeToArray('A1:' . $highestcolumn . "1");
    $body = $sheet->rangeToArray('A2:' . $highestcolumn . $highestrow);

    array_walk($body, function (&$row) use($titles) { $row = array_combine($titles[0], $row); });

    return $body;
}

这篇关于PHP函数,需要输入一个excel文件的名称,并返回关联数组的数组与Excel文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆