是否可以将XLSX文件的每一行导入PHP数组中的一行?
答案 0 :(得分:14)
我用这个:
include 'simplexlsx.class.php';
$xlsx = @(new SimpleXLSX('myFile.xlsx'));
$data = $xlsx->rows();
您可以here中的simplexslx。
<强>更新强>
显然上面的链接不再起作用了。您现在可以使用this。 (谢谢@Basti)
答案 1 :(得分:1)
使用PHPExcel库可以解决问题:
$data = [];
$type = PHPExcel_IOFactory::identify($filepath);
$objReader = PHPExcel_IOFactory::createReader($type);
$objPHPExcel = $objReader->load($filepath);
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
foreach($rowIterator as $row){
$cellIterator = $row->getCellIterator();
foreach ($cellIterator as $cell) {
$data[$row->getRowIndex()][$cell->getColumn()] = $cell->getCalculatedValue();
}
}
其中$ filepath - xls或xlsx文件的路径。
答案 2 :(得分:0)
include 'vendor/autoload.php';
if($_FILES["import_excel"]["name"] != '')
{
$allowed_extension = array('xls', 'csv', 'xlsx');
$file_array = explode(".", $_FILES["import_excel"]["name"]);
$file_extension = end($file_array);
if(in_array($file_extension, $allowed_extension))
{
$file_name = time() . '.' . $file_extension;
move_uploaded_file($_FILES['import_excel']['tmp_name'], $file_name);
$file_type = \PhpOffice\PhpSpreadsheet\IOFactory::identify($file_name);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($file_type);
$spreadsheet = $reader->load($file_name);
unlink($file_name);
$data = $spreadsheet->getActiveSheet()->toArray();
foreach($data as $row)
{
$insert_data = array(
':test1' => $row[0],
':test2' => $row[1],
':test3' => $row[2],
':test4' => $row[3]
);
};
$query = "
INSERT INTO post
( test1, test2, test3, test4)
VALUES
( :test1, :test2, :test3, :test4)
";
$statement = $connect->prepare($query);
$statement->execute($insert_data);
}
echo "succes";
}else{
echo "only xls,csv,xlsx are allowed";
}