我正在尝试使用PHPSpreadsheet composer包从Excel电子表格导入值。但它一直缺少excel文件中的一些行。可能是什么原因?
代码运行完美,并在mysql表中插入数据。
假设我有50行数据(除了标题行),下面的查询只会插入45行数据。
请查看以下代码: 代码:
$inputFileType = ''; //file extention
//check file extension
if(pathinfo($file, PATHINFO_EXTENSION) == 'csv'){
$inputFileType = 'Csv';
} elseif(pathinfo($file, PATHINFO_EXTENSION) == 'xls'){
$inputFileType = 'Xls';
} elseif(pathinfo($file, PATHINFO_EXTENSION) == 'xlsx'){
$inputFileType = 'Xlsx';
}
//select file posted for inserting data
$inputFileName = $_FILES["file"]["tmp_name"];
$reader = IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($inputFileName);
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
for($row = 6; $row <= count($sheetData); $row++)
{
$nr_orgUnit = $hospital;
$inserted_by = $inserted_by;
$xx = "'" . $nr_orgUnit . "', '" . implode("','", $sheetData[$row]) . "', '" . $inserted_by . "'";
$stmt = $conn->query("INSERT INTO `ag_nacp_item_1_7`(`nr_orgUnit`, `PatientID`, `Age`, `Sex`, `NumberTimesStartedTBRX`, `Pregnant`, `KP`, `inserted_by`) VALUES($xx)");
}
if($stmt){
echo '<div class="alert alert-success">
<strong>Success!</strong> Report uploaded.
</div>';
} else {
echo '
<div class="alert alert-warning">
<strong>Error!</strong> ' . $sql . '<br>' . $conn->error .'
</div>';
}