为了在 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“上传.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 - DEAD PHPExcel 最后一个版本,1.8.1,于 2015 年发布。该项目于 2017 年正式弃用,并于 2019 年永久存档。
该项目已多年未维护,不得再使用。所有用户都必须迁移到其直接继承者 PhpSpreadsheet 或其他替代方案。