gpt4 book ai didi

PHP - PHPExcel 范围到数组忽略空列

转载 作者:行者123 更新时间:2023-12-02 01:52:24 25 4
gpt4 key购买 nike

下面是 Excel 工作表,我想将范围 A1:D4 放入 PHP 数组中。

enter image description here

为此,我使用 PHPExcel ,如下所示。我有一个简单的 Excel 类:

Excel.php

public function __construct($file)
{
if ($file instanceof \SplFileInfo) {
$filename = $file->getRealPath();
} else {
$filename = $file;
}

$this->objPHPExcel = \PhpOffice\PhpSpreadsheet\IOFactory::load($filename);
$this->objPHPExcel->setActiveSheetIndex(0);
}

然后我有一个简单的函数来获取范围,使用 rangeToArray()功能:

public function getRange($range)
{
$spreadsheet = $this->objPHPExcel;
return $spreadsheet->rangeToArray($range,' ', true, true, true);

}

$excel = new ExcelManipulator(storage_path() . '/app/temp_files/myfile.xlsx');
$array = $excel->getRange($range);

return response()->json(['result' => $array], 200);

现在的问题是上面的函数“切换”了列。请参阅下面的输出:

[
'1': [
"A": " ",
"B": " ",
"C": " ",
"D": " "
],
'2': [
"A": "Company",
"B": "Acme Inc",
"C": " ",
"D": " "
],
'3': [
"A": "Address",
"B": "New York",
"C": " ",
"D": " "
],
'4': [
"A": " ",
"B": " ",
"C": " ",
"D": " "
]
]

正如您在 23 行的数组中看到的,companyaddress 文本开始已经在A 列 中,而它们应该从B 列

开始

最佳答案

你可以试试

function getRange($range, $inputFileName)
{
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFilename);
return $spreadsheet->getActiveSheet()->rangeToArray($range," ", true, true, true);

}
$inputFilename = storage_path() . '/app/temp_files/myfile.xlsx';
$array = getRange($range, $inputFilename);

关于PHP - PHPExcel 范围到数组忽略空列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59369572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com