$file = 'upload/'.$filename;
//read file from path
$objPHPExcel = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel->setReadDataOnly(true);
$objPHPExcel = $objPHPExcel->load($file);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
//$row = array();
for($row=1; $row <= $highestRow; ++$row){
for($col=0; $col <= $highestColumnIndex; ++$col){
$row[$col] = $objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
}
}
echo "<pre>";print_r($row[$col]);echo "</pre>";
这是$row
的问题所在。它告诉我$row
的值类型是标量。我正在使用数组将excel中的任何单元格放入我的html中。
我需要将此数据放入我的mysql数据库中。我正在使用Excel工作表,示例工作表是:
(nama_lengkap,tempat_lahir,provinsi_lahir)--->作为标题 (Anak pertama,雅加达,DKI) (Anak kedua,万隆,贾瓦·巴拉特) (Anak ketiga,三宝垄,贾瓦·登加)
想象表数据:
| id | nama | tempat_lahir | provinsi_lahir |
|----|-------------|--------|-------|
| 1 | John | Semarang | Jawa tengah |
| 2 | Meresa | Bandung | Jawa Barat |
| 3 | Mike | Jakarta | Dki |
答案 0 :(得分:0)
另一个一切都很好。但是,您正在$row
循环中一次又一次地覆盖for
中的值。代码如下:
$file = 'upload/' . $filename;
//read file from path
$objPHPExcel = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel->setReadDataOnly(true);
$objPHPExcel = $objPHPExcel->load($file);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$excelData = array();
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
$excelData[$row][$col] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
}
echo "<pre>";
print_r($excelData);
echo "</pre>";
采用数组$excelData
,并将excel工作表中的数据存储在其中。