php - 在 Php Excel 中获取带有列名的单元格值

标签 php phpexcel

这是我的 excel 表 Excel Sheet它有很多专栏,但我正在分解以便于理解问题

我正在使用 PHP Excel 读取 Excel 工作表并使用 rangeToArray() 给我所有来自 excel 的行,但我希望输出为

Column as Key:Cell Value as Value

目前我的输出为

Col Index :Cell Value

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

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 中很容易做到

从第一行中检索你想要的标题/键

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

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

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 - 在 Php Excel 中获取带有列名的单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32526534/

相关文章:

PHPExcel 将文件保存在文件夹中或打开 excel

PHPExcel - 应用数字格式时性能严重下降

javascript - 如何检测 Javascript 中是否设置了 cookie?

php - 有没有一种快速的方法可以为所有 CSS 类和 ID 添加前缀?

php - CakePHP GET 请求仅在部分时间失败

php - 如何向单元格添加符号而不影响使用同一单元格的公式?

PHP - session 变量不在页面之间保存

php - 直接向mysql数据库中插入值

PHPExcel 公式在 HTML 中不起作用

php - 无法在服务器上创建 Excel 文件