我正在尝试将数据从Excel插入到我的数据库表中,但它给了我一个致命的错误。我不知道如何正确使用PHPExcel库。这是错误:
致命错误:未捕获PHPExcel_Reader_Exception:无法打开export.xls进行读取!文件不存在。在C:\ xampp \ htdocs \ project \ PHPExcel \ Reader \ Excel5.php:433堆栈跟踪:#0 C:\ xampp \ htdocs \ project \ PHPExcel \ IOFactory.php(269):PHPExcel_Reader_Excel5-> canRead('export .xls')#1 C:\ xampp \ htdocs \ project \ PHPExcel \ IOFactory.php(191):PHPExcel_IOFactory :: createReaderForFile('export.xls')#2 C:\ xampp \ htdocs \ project \ import_subject_allocation.php( 9):在第433行的C:\ xampp \ htdocs \ project \ PHPExcel \ Reader \ Excel5.php中抛出PHPExcel_IOFactory :: load('export.xls')#3 {main}
我必须将一些数据从excel文件上传到数据库,我无法通过CSV文件上传。我宁愿不将XLS文件转换为复杂且耗时的CSV文件
<?php
require_once 'server_config.php';
$conn=myconnection();
include "PHPExcel/IOFactory.php";
require 'PHPExcel.php';
if(isset($_REQUEST["Import"])) {
echo $filename = $_FILES["file"]["name"];
if ($_FILES["file"]["size"] > 0) {
$objPHPExcel=PHPExcel_IOFactory::load($filename);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$highestrow= $worksheet->getHighestRow();
for($row=2;$row<=$highestrow;$row++)
{
$name=mysqli_real_escape_string($conn , $worksheet->getCellByColumnAndRow(0,$row)->getValue());
$email=mysqli_real_escape_string($conn , $worksheet->getCellByColumnAndRow(1,$row)->getValue());
$query = "insert into test (`name`,`email`) values ('$name','$email')";
$n= iud($query);
if($n){
echo "inserted";
}else
echo "not";
}
}
}
}
?>
<form enctype="multipart/form-data" method="post" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">Only Excel/CSV File Import.</p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
答案 0 :(得分:0)
在PHPExcel_IOFactory::load
您需要将文件路径作为参数而不仅仅是文件名,因此请进行连接以获得:$filePath."/".$fileName