php - 尝试访问 PhpSpreadsheet 中 null 类型值的数组偏移量
问题描述
我正在使用 ajax 上传 excel 表并将数据插入数据库。我测试过,它正在工作。
但是我在网络选项卡中遇到了一个错误。
注意:尝试访问第 1934 行 /opt/lampp/htdocs/test/assets/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php 中 null 类型值的数组偏移量
我在 excel 工作表中只有 1 条记录,当我导入工作表时,我得到三行(第一行是正确的,其余两行是 NULL)。
我正在使用下面的代码
HTML
<form method="post" id="import_excelsheet" name="import_excelsheet" enctype="multipart/form-data">
<div class="form-group"><input type="file" name="list21" /></div>
<input type="hidden" name="action" value="list21Import">
<input type="submit" name="listImport21" id="list_import" class="btn btn-primary" value="Import list" />
</form>
JS
$('#import_excelsheet').validate({
rules: {
list21: {
required: true,
extension: "xlsx|xls|xlsm|csv"
}
},
messages: {
list21: {
required: "file .xlsx, .xlsm, .xls, .csv only.",
extension: "Please upload valid file formats .xlsx, .xlsm, .xls, .csv only."
}
},
submitHandler: function(form) {
$.ajax({
url:"process.php",
method:"POST",
data:new FormData($('#import_excelsheet')[0]),
contentType:false,
cache:false,
processData:false,
success:function(data)
{
alert("success");
//location.reload();
}
}) ;
}
});
Process.php(这只是一个函数)
function list21Import($pdo){
if($_FILES["list21"]["name"] != '')
{
$allowed_extension = array('xls', 'csv', 'xlsx');
$file_array = explode(".", $_FILES["list21"]["name"]);
$file_extension = end($file_array);
if(in_array($file_extension, $allowed_extension))
{
$file_name = time() . '.' . $file_extension;
move_uploaded_file($_FILES['list21']['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();
//print_r($data);
$isheader = 0;
foreach($data as $row)
{
$social['fb'] = trim($row[5]);
$social['twitter'] = trim($row[6]);
$social['linkedin'] = trim($row[7]);
$social['youtube'] = trim($row[8]);
$social['instagram'] = trim($row[9]);
$social_links = serialize($social);
if($isheader > 0) {
$insert_data = array(
':name' => $row[0],
':email' => $row[1],
':content' => $row[2],
':website' => $row[3],
':logo' => $row[4],
':social_links' =>$social_links,
);
$query = "INSERT INTO `list21`(`name`, `email`, `content`, `website`, `logo`, `social_links`) VALUES (:name,:email,:content,:website,:logo,:social_links)";
$statement = $pdo->prepare($query);
$statement->execute($insert_data);
}
else {
$isheader = 1;
}
} // foreach
$message = '<div class="alert alert-success">Data Imported Successfully</div>';
} //in_array
else
{
$message = '<div class="alert alert-danger">Only .xls .csv or .xlsx file allowed</div>';
}
}
else
{
$message = '<div class="alert alert-danger">Please Select File</div>';
}
echo $message;
}
你能帮我解决这个问题吗?
解决方案
推荐阅读
- reactjs - react中基于路由条件渲染组件
- java - ConnectException:无法在 Android 9 中连接到.....
- hybris - 请给我一个关于使用 [batchmode =true] 和不使用 [batchmode =true] 的 ImpEx 更新的示例
- mysql - 如何从 Linux shell 脚本运行 MySQL 过程并将其输出存储在文件中
- ios - Xamarin.UITest 无法联系在应用程序中运行的测试后端
- select - 如何从变量中采样项目
- django - 如何将模型数据库的数据填充到 django 的下拉列表中
- linux - 在 Linux Mint 19.1 中禁用星号
- blender - 如何删除对象的一部分?
- php - Nginx :只允许从本地主机访问文件夹