获取列名在 Php Excel 中的单元格值
Get Cell Value with column name in Php Excel
这是我的 excel sheet 它有很多专栏,但为了便于理解问题,我正在分解
我正在阅读 Excel Sheet 使用 PHP Excel 并使用 rangeToArray() 给我所有来自 excel 的行,但我希望输出为
Column as Key:Cell Value as Value
目前我得到的输出是
Col Index :Cell Value
所以我的问题是 Php Excel 中哪个 return 数组具有列名和它的单元格值?
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/keys
$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]);
}
foreach ($cell_collection as $cell)
{
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
if ($row == 1)
{
$header[$row][$column] = $data_value;
}
else
{
$arr_data[$row][$column] = $data_value;
}
}
我创建这个功能是为了不要像A1,A2,B1,B2那样写参考文献的标题。并且有效。
public function createExcel($titulos, $datos, $tituloLibro = 'The title')
{
$phpExcelObject = //instance it;
$phpExcelObject->getProperties()->setCreator("Your name")
->setLastModifiedBy("yourweb.com")
->setTitle($tituloLibro)
->setSubject($tituloLibro)
->setDescription($tituloLibro)
->setKeywords("")
->setCategory($tituloLibro);
$i = 0;
$phpExcelObject->setActiveSheetIndex(0);
foreach ($titulos as $titulo)
{
$phpExcelObject->getActiveSheet()->getCellByColumnAndRow($i,1)->setValue($titulo);
$i++;
}
$j = 2;
foreach ($datos as $filas)
{
$i = 0;
foreach ($filas as $fila)
{
$phpExcelObject->getActiveSheet()->getCellByColumnAndRow($i,$j)->setValue($fila);
$i++;
}
$j++;
}
// your logic
}
这是我的 excel sheet
我正在阅读 Excel Sheet 使用 PHP Excel 并使用 rangeToArray() 给我所有来自 excel 的行,但我希望输出为
Column as Key:Cell Value as Value
目前我得到的输出是
Col Index :Cell Value
所以我的问题是 Php Excel 中哪个 return 数组具有列名和它的单元格值?
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/keys$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]);
}
foreach ($cell_collection as $cell)
{
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
if ($row == 1)
{
$header[$row][$column] = $data_value;
}
else
{
$arr_data[$row][$column] = $data_value;
}
}
我创建这个功能是为了不要像A1,A2,B1,B2那样写参考文献的标题。并且有效。
public function createExcel($titulos, $datos, $tituloLibro = 'The title')
{
$phpExcelObject = //instance it;
$phpExcelObject->getProperties()->setCreator("Your name")
->setLastModifiedBy("yourweb.com")
->setTitle($tituloLibro)
->setSubject($tituloLibro)
->setDescription($tituloLibro)
->setKeywords("")
->setCategory($tituloLibro);
$i = 0;
$phpExcelObject->setActiveSheetIndex(0);
foreach ($titulos as $titulo)
{
$phpExcelObject->getActiveSheet()->getCellByColumnAndRow($i,1)->setValue($titulo);
$i++;
}
$j = 2;
foreach ($datos as $filas)
{
$i = 0;
foreach ($filas as $fila)
{
$phpExcelObject->getActiveSheet()->getCellByColumnAndRow($i,$j)->setValue($fila);
$i++;
}
$j++;
}
// your logic
}