如何防止PHP excel导入删除列中的前导零?
我在中尝试了将XLSX文件导入数据库的教程(使用PHP) 但问题是,如果列值以前导零开始,在导入后,我发现前导零已被删除 如何预防 顺便说一下,要导入的特定列的phpmyadmin表的结构是文本列,而不是整数 我尝试添加“”以将文件视为字符串,但未显示成功如何防止PHP excel导入删除列中的前导零?,php,excel,import,Php,Excel,Import,我在中尝试了将XLSX文件导入数据库的教程(使用PHP) 但问题是,如果列值以前导零开始,在导入后,我发现前导零已被删除 如何预防 顺便说一下,要导入的特定列的phpmyadmin表的结构是文本列,而不是整数 我尝试添加“”以将文件视为字符串,但未显示成功 $conn = mysqli_connect("localhost","root","test","phpsamples"); require_once('vendor/php-excel-reader/excel_reader2.php')
$conn = mysqli_connect("localhost","root","test","phpsamples");
require_once('vendor/php-excel-reader/excel_reader2.php');
require_once('vendor/SpreadsheetReader.php');
if (isset($_POST["import"]))
{
$allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
if(in_array($_FILES["file"]["type"],$allowedFileType)){
$targetPath = 'uploads/'.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
$Reader = new SpreadsheetReader($targetPath);
$sheetCount = count($Reader->sheets());
for($i=0;$i<$sheetCount;$i++)
{
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$name = "";
if(isset($Row[0])) {
$name = mysqli_real_escape_string($conn,$Row[0]);
}
$description = "";
if(isset($Row[1])) {
$description = mysqli_real_escape_string($conn,$Row[1]);
}
if (!empty($name) || !empty($description)) {
$query = "insert into tbl_info(name,description) values('".$name."','".$description."')";
$result = mysqli_query($conn, $query);
if (! empty($result)) {
$type = "success";
$message = "Excel Data Imported into the Database";
} else {
$type = "error";
$message = "Problem in Importing Excel Data";
}
}
}
}
}
else
{
$type = "error";
$message = "Invalid File Type. Upload Excel File.";
}
}
$conn=mysqli_connect(“localhost”、“root”、“test”、“phpsamples”);
只需一次('vendor/php excel reader/excel_reader2.php');
一次需要_('vendor/SpreadsheetReader.php');
如果(isset($_POST[“导入”]))
{
$allowedFileType=['application/vnd.ms excel'、'text/xls'、'text/xlsx'、'application/vnd.openxmlformats of icedocument.spreadsheetml.sheet'];
if(在数组中($文件[“文件”][“类型”],$allowedFileType)){
$targetPath='uploads/'。$_文件['file']['name'];
移动上传的文件($文件['file']['tmp\u名称],$targetPath);
$Reader=新的电子表格阅读器($targetPath);
$sheetCount=count($Reader->sheets());
对于($i=0;$iChangeSheet($i);
foreach($Reader作为$Row)
{
$name=“”;
如果(isset($Row[0])){
$name=mysqli\u real\u escape\u字符串($conn,$Row[0]);
}
$description=“”;
如果(isset($Row[1])){
$description=mysqli\u real\u escape\u字符串($conn,$Row[1]);
}
如果(!empty($name)| |!empty($description)){
$query=“插入tbl_信息(名称、描述)值(‘“$name.”、‘“$description.”);
$result=mysqli\u查询($conn,$query);
如果(!空($result)){
$type=“成功”;
$message=“导入数据库的Excel数据”;
}否则{
$type=“error”;
$message=“导入Excel数据时出现问题”;
}
}
}
}
}
其他的
{
$type=“error”;
$message=“文件类型无效。请上载Excel文件。”;
}
}
添加一个前导撇号(当然,仅当$description
以0
开头时),它将强制Excel以文本形式读取单元格
if ($description[0] === '0') {
$description = "'" . $description;
}
添加前导撇号(当然,仅当$description
以0
开头时),它将强制Excel以文本形式读取单元格
if ($description[0] === '0') {
$description = "'" . $description;
}
首先,您是否仔细检查了原始XLSX中是否存在前导零?其次,根据您正在使用的链接。该库依赖于解析XLSX文件。这两个库都已被放弃且存在漏洞。后者甚至托管在谷歌代码中!首先,您是否仔细检查了前导零是否实际存在你在原始的XLSX中吗?其次,根据你使用的链接。该库依赖于解析XLSX文件。这两个库都已被废弃,且存在漏洞。后者甚至托管在谷歌代码中!我现在将尝试并测试导入,如果导入不成功,将给出反馈。我现在将尝试并测试导入,如果导入成功,将给出反馈不成功