在Php Excel中获取具有列名称的单元格值 [英] Get Cell Value with column name in Php Excel

查看:390
本文介绍了在Php Excel中获取具有列名称的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Excel工作表它有很多列,但为了便于理解我正在分解

This is my excel sheet it has got lots of columns but i'm breaking down for ease in understanding the question

我正在使用 PHP Excel 并使用rangeToArray()读取Excel工作表,这将使我从excel,但我希望输出为

I'm reading Excel Sheet using PHP Excel and using rangeToArray() which give me all row from excel but i want the output as

列作为键:单元格值作为值

Column as Key:Cell Value as Value

目前,我的输出为

颜色指数:单元格值

Col Index :Cell Value

所以我的问题是Php Excel中的哪个函数返回带有列名及其单元格值的数组?

So my Question is which is that function in Php Excel which return array with Column name and it Cell value?

try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
    die('Error loading file"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();
for ($row = 2; $row <= $highestRow; $row++){ 
    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                            NULL,
                                            TRUE,
                                            FALSE);
    printArr($rowData);
    printArr("-------");

}

我得到的输出为

Array
(
    [0] => Array
        (
            [0] => 27745186
            [1] => 42058
            [2] => DELL INTERNATIONAL SERVICES INDIA PVT LTD
            ...
            ...
         )
    [1] => Array
        (
            [0] => 27745186
            [1] => 42058
            [2] => DELL INTERNATIONAL SERVICES INDIA PVT LTD
            ...
            ...
         )
)

期望输出

Array
(
    [0] => Array
        (
            [Invoice_no] => 27745186
            [Invoice Date] => 42058
            [Description] => DELL INTERNATIONAL SERVICES INDIA PVT LTD
            ...
            ...
         )
    [1] => Array
        (
            [Invoice_no] => 27745186
            [Invoice Date] => 42058
            [Description] => DELL INTERNATIONAL SERVICES INDIA PVT LTD
            ...
            ...
         )
)

推荐答案

PHPExcel中没有什么魔术,但是在标准PHP中很容易做到

There's nothing magic in PHPExcel, but it's pretty easy to do in standard PHP

从第一行检索所需的标题/键

Retrieve the headings/keys that you want from the first row

$headings = $sheet->rangeToArray('A1:' . $highestColumn . 1,
                                            NULL,
                                            TRUE,
                                            FALSE);

然后将标准数字键重置为每个数据行的读取循环内的标题值

Then reset the standard numeric keys to be the headings values inside your reading loop for each data row

for ($row = 2; $row <= $highestRow; $row++){ 
    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                            NULL,
                                            TRUE,
                                            FALSE);
    $rowData[0] = array_combine($headings[0], $rowData[0]);
}

这篇关于在Php Excel中获取具有列名称的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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