从另一个目录导入并显示PHP中的excel文件.我想阅读excel并展示它的内容


Import and display excel file in PHP from another directory. I want to read excel and display its conten

我想从另一个目录导入PHP中的excel文件。我想阅读excel并显示其内容,但它在另一个目录中,我得到了一个错误

Notice: Undefined index: file in F:'Xampp'htdocs'upload.php on line 3

import.php

<form action="upload.php" >
<input name="file" type="file">
<input name="submit" type="submit">
</form>

上传.php

<?php 
$uploadedStatus = 0; 
echo $_FILES["file"]["name"];
?>

为了在php中显示excel,您可以像这样使用PHPExcel:

include 'PHPExcel/IOFactory.php';
$inputFileType = 'Excel5';
$inputFileName = 'MyExcelFile.xls';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->save('php://output');
exit;

要上传文件,你可以使用简单的文件上传,如下所示:

PHP"upload.PHP":

<?php
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        if(isset($_POST["submit"])) {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
              echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
            } else {
               echo "Sorry, there was an error uploading your file.";
              }
        }
    ?>

HTML表单:

<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>

更新:

PHPExcel-DEADPHPExcel的上一个版本1.8.1发布于2015年。该项目于2017年被正式否决,并于2019年永久存档。

该项目已多年未维护,不得再使用。所有用户都必须迁移到其直接继任者PhpSpreadsheet或其他替代方案。

确保表单具有用于处理文件上载的enctype属性。

<form action="upload.php" enctype="multipart/form-data">
    <input name="file" type="file">
    <input name="submit" type="submit">
</form>

参考文献:

  1. 使用PHP导入excel文件
  2. PHP Excel阅读器