PHP - PHPExcel Range to Array 忽略空列
PHP - PHPExcel Range to Array ignores empty column
我在 Excel sheet 下方,我想将范围 A1:D4
放入 PHP 数组中。
为此,我使用 PHPExcel
,如下所示。我有一个简单的 Excel
class:
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);
现在的问题是上面函数"switches"的列。请参阅以下输出:
[
'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": " "
]
]
正如您在行 2
和 3
的数组中看到的那样,company 和 address文本已经从 Column A
开始,而它们应该从 Column 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 7.2.26 和 phpoffice/phpspreadsheet 1.10.1 时效果很好。我创建了一个 xlsx 文件,其单元格值与您的相同。
$excel = new Excel('file.xlsx');
$range = 'A1:D4';
$array = $excel->getRange($range);
var_dump($array);
class Excel
{
private $objPHPExcel;
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);
}
public function getRange($range)
{
$spreadsheet = $this->objPHPExcel->getActiveSheet();
return $spreadsheet->rangeToArray($range,' ', true, true, true);
}
}
这是输出:
array(4) {
[1] =>
array(4) {
'A' =>
string(2) " "
'B' =>
string(2) " "
'C' =>
string(2) " "
'D' =>
string(2) " "
}
[2] =>
array(4) {
'A' =>
string(2) " "
'B' =>
string(7) "Company"
'C' =>
string(8) "Acme Inc"
'D' =>
string(2) " "
}
[3] =>
array(4) {
'A' =>
string(2) " "
'B' =>
string(7) "Address"
'C' =>
string(8) "New York"
'D' =>
string(2) " "
}
[4] =>
array(4) {
'A' =>
string(2) " "
'B' =>
string(2) " "
'C' =>
string(2) " "
'D' =>
string(2) " "
}
}
我在 Excel sheet 下方,我想将范围 A1:D4
放入 PHP 数组中。
为此,我使用 PHPExcel
,如下所示。我有一个简单的 Excel
class:
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);
现在的问题是上面函数"switches"的列。请参阅以下输出:
[
'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": " "
]
]
正如您在行 2
和 3
的数组中看到的那样,company 和 address文本已经从 Column A
开始,而它们应该从 Column 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 7.2.26 和 phpoffice/phpspreadsheet 1.10.1 时效果很好。我创建了一个 xlsx 文件,其单元格值与您的相同。
$excel = new Excel('file.xlsx');
$range = 'A1:D4';
$array = $excel->getRange($range);
var_dump($array);
class Excel
{
private $objPHPExcel;
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);
}
public function getRange($range)
{
$spreadsheet = $this->objPHPExcel->getActiveSheet();
return $spreadsheet->rangeToArray($range,' ', true, true, true);
}
}
这是输出:
array(4) {
[1] =>
array(4) {
'A' =>
string(2) " "
'B' =>
string(2) " "
'C' =>
string(2) " "
'D' =>
string(2) " "
}
[2] =>
array(4) {
'A' =>
string(2) " "
'B' =>
string(7) "Company"
'C' =>
string(8) "Acme Inc"
'D' =>
string(2) " "
}
[3] =>
array(4) {
'A' =>
string(2) " "
'B' =>
string(7) "Address"
'C' =>
string(8) "New York"
'D' =>
string(2) " "
}
[4] =>
array(4) {
'A' =>
string(2) " "
'B' =>
string(2) " "
'C' =>
string(2) " "
'D' =>
string(2) " "
}
}